Android HTTP GET错误

27 浏览
0 Comments

Android HTTP GET错误

抱歉,我是Java和Android的新手。\n我想了解为什么它总是出现错误。\n谢谢!\n

try {
            HttpClient client = new DefaultHttpClient();  
            String getURL = "http://www.google.com";
            HttpGet get = new HttpGet(getURL);
            HttpResponse responseGet = client.execute(get);  
            HttpEntity resEntityGet = responseGet.getEntity();  
            if (resEntityGet != null) {  
            Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
             }
    } catch (Exception e) {
        //e.printStackTrace();
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
    }

\n导入\n

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

\n是的,我已经输入了网络权限。\n



        
    

0
0 Comments

问题原因:在Android中使用HTTP GET请求时出现错误。

解决方法:修改代码,使用正确的HTTP GET请求方法。

在Android开发中,我们经常需要使用HTTP请求与服务器进行通信。下面是一段用于发送HTTP请求的代码示例:

public class RequestClient extends AsyncTask<String, Void, String>{
    Context context;
    public RequestClient(Context c) {
        context = c;
    }
    protected void onPreExecute() {
        super.onPreExecute();
    }
    protected String doInBackground(String... aurl){
    String responseString="";
    HttpClient client = null;
    try {
         client = new DefaultHttpClient();  
         HttpGet get = new HttpGet(LoginActivity.url);
         HttpResponse responseGet = client.execute(get);  
         HttpEntity resEntityGet = responseGet.getEntity();  
         if (resEntityGet != null) {  
             responseString = EntityUtils.toString(resEntityGet);
             Log.i("GET RESPONSE", responseString.trim());
         }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
    }
        Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
        client.getConnectionManager().shutdown();
     return responseString.trim();
    }
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
        }
}

然而,在使用这段代码发送HTTP GET请求时,可能会遇到错误。为了解决这个问题,我们需要修改代码中的HTTP GET请求方法。

具体的修改方法如下:

public class RequestClient extends AsyncTask<String, Void, String>{
    Context context;
    public RequestClient(Context c) {
        context = c;
    }
    protected void onPreExecute() {
        super.onPreExecute();
    }
    protected String doInBackground(String... aurl){
    String responseString="";
    HttpURLConnection connection = null;
    try {
         URL url = new URL(LoginActivity.url);
         connection = (HttpURLConnection) url.openConnection();
         connection.setRequestMethod("GET");
         connection.connect();
         int responseCode = connection.getResponseCode();
         if (responseCode == HttpURLConnection.HTTP_OK) {
             InputStream inputStream = connection.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
             StringBuilder stringBuilder = new StringBuilder();
             String line;
             while ((line = reader.readLine()) != null) {
                 stringBuilder.append(line);
             }
             responseString = stringBuilder.toString();
             inputStream.close();
             reader.close();
         }
    } catch (Exception e) {
        Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
    }
        Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
        if (connection != null) {
            connection.disconnect();
        }
     return responseString.trim();
    }
    protected void onPostExecute(String response) {
         super.onPostExecute(response); 
        }
}

通过修改后的代码,我们可以正确地发送HTTP GET请求,并从服务器获取响应数据。这样就解决了Android中使用HTTP GET请求时出现的错误。

0
0 Comments

问题原因:在UI线程中进行了http请求。

解决方法:将请求放在另一个线程中进行。同时,从try块和catch块中删除Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();,并在try块和catch块中使用Log.d("OK","Ok");来查看结果。如果想要使用Toast,则应该使用runOnUiThread(new Runnable() {public void run() {Toast.makeText(.....).show();}});的方式来使用。

0