【问题标题】:Android HTTP GET errorAndroid HTTP GET 错误
【发布时间】:2013-07-12 13:35:24
【问题描述】:

对不起,我是 java 和 android 世界的新手。 我想了解为什么它总是响应错误。 谢谢!

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();
    }

导入

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;

是的,我进入了互联网许可

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testhttp"
    android:versionCode="1"
    android:versionName="1.0" >

        <uses-permission android:name="android.permission.INTERNET" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

【问题讨论】:

  • 使用 LogCat 检查与您的“错误”相关的 Java 堆栈跟踪。如果您不了解堆栈跟踪,请编辑您的问题并将其发布在此处。
  • 取消注释e.printStackTrace(),看看logcat有什么异常

标签: java android http get


【解决方案1】:

似乎您正试图在UI 线程 中发出http 请求。

将您的请求放在另一个线程中。

【讨论】:

  • @user2068102 删除 Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();来自 try 块以及来自 catch 块并使用 Log.d("OK","Ok");在 try 和 catch 块中并在 logCat 中查看结果!
  • 如果你想使用 Toast 而不是使用它:runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(.....).show(); } });
【解决方案2】:

您也可以使用此代码发出 HTTP 请求:

public class RequestClient extends AsyncTask<String, Void, String>{
        Context context;

        public RequestClient(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        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();

        }


        @Override
        protected void onPostExecute(String response) {
             super.onPostExecute(response); 
            }
    }

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 2019-04-17
    • 2018-10-26
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 2018-03-21
    • 2017-06-27
    相关资源
    最近更新 更多