为什么在HttpResponse response = client.execute(request)line之后,furthur执行会停止?

时间:2014-12-22 17:43:29

标签: android apache api http-response-codes

import java.io.BufferedReader; 
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class GetPostEx {

public String getInternetData() throws Exception{

    //String source_prog;
    //int lang_number;
    //BufferedReader in = null;
    String ret=null;
    try{

        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost("Some server link");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
        nameValuePairs.add(new BasicNameValuePair("source", "print 1"));
        nameValuePairs.add(new BasicNameValuePair("lang", "5"));
        nameValuePairs.add(new BasicNameValuePair("testcases", "[\"1\"]"));
        nameValuePairs.add(new BasicNameValuePair("api_key", "Some key"));
         request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        Log.e("error", "before HTTPResponse");
        HttpResponse response = client.execute(request);
        Log.e("error", "Aftr HTTPResponse");
        ret = EntityUtils.toString(response.getEntity());
        }catch (Exception e){
            //Log.e("error","error!!");
        e.printStackTrace();
    }
    return ret;
}

}  

我在调用GetPostEx类的其他活动(AndroidHTTPRequestsActivity)中使用的代码是: -

GetPostEx test = new GetPostEx();
    try{
        returned = test.getInternetData();
} catch(Exception e){ 
            Log.e("error", "yo") ;
            e.printStackTrace();
}

我正在尝试使用API​​从服务器创建一个需要一些HTTP响应的android应用程序。这是我获取HTTP响应的代码。但执行在行 HttpResponse response = client.execute(请求)之后停止); :此行之后的logcat没有显示错误,而上面显示的错误正在显示。 我无法弄清楚这背后的原因。

以下行是logcat中的堆栈跟踪详细信息: -

12-22 14:18:58.658:E /错误(1335):在HTTPResponse之前

12-22 14:18:59.448:W / System.err(1335):android.os.NetworkOnMainThreadException

12-22 14:18:59.448:W / System.err(1335):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

12-22 14:18:59.458:W / System.err(1335):at java.net.InetAddress.lookupHostByName(InetAddress.java:385)

12-22 14:18:59.458:W / System.err(1335):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)

12-22 14:18:59.458:W / System.err(1335):at java.net.InetAddress.getAllByName(InetAddress.java:214)

12-22 14:18:59.458:W / System.err(1335):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)

12-22 14:18:59.458:W / System.err(1335):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

12-22 14:18:59.458:W / System.err(1335):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)

12-22 14:18:59.478:W / System.err(1335):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)

12-22 14:18:59.478:W / System.err(1335):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

12-22 14:18:59.478:W / System.err(1335):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

12-22 14:18:59.478:W / System.err(1335):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

12-22 14:18:59.478:W / System.err(1335):at com.us.onlinecompilr.GetPostEx.getInternetData(GetPostEx.java:37)

12-22 14:18:59.478:W / System.err(1335):at com.us.onlinecompilr.AndroidHTTPRequestsActivity.onCreate(AndroidHTTPRequestsActivity.java:29)

12-22 14:18:59.478:W / System.err(1335):在android.app.Activity.performCreate(Activity.java:5243)

12-22 14:18:59.478:W / System.err(1335):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

12-22 14:18:59.478:W / System.err(1335):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

12-22 14:18:59.488:W / System.err(1335):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)

12-22 14:18:59.488:W / System.err(1335):在android.app.ActivityThread.access $ 700(ActivityThread.java:135)

12-22 14:18:59.488:W / System.err(1335):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1397)

12-22 14:18:59.488:W / System.err(1335):在android.os.Handler.dispatchMessage(Handler.java:102)

12-22 14:18:59.488:W / System.err(1335):在android.os.Looper.loop(Looper.java:137)

12-22 14:18:59.488:W / System.err(1335):在android.app.ActivityThread.main(ActivityThread.java:4998)

12-22 14:18:59.488:W / System.err(1335):at java.lang.reflect.Method.invokeNative(Native Method)

12-22 14:18:59.488:W / System.err(1335):at java.lang.reflect.Method.invoke(Method.java:515)

12-22 14:18:59.488:W / System.err(1335):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)

12-22 14:18:59.488:W / System.err(1335):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

12-22 14:18:59.488:W / System.err(1335):at dalvik.system.NativeStart.main(Native Method)

12-22 14:18:59.588:I / Choreographer(1335):跳过641帧!应用程序可能在其主线程上做了太多工作。

12-22 14:19:03.278:I / Choreographer(1335):跳过33帧!应用程序可能在其主线程上做了太多工作。

12-22 14:19:06.188:I / Choreographer(1335):跳过112帧!应用程序可能在其主线程上做了太多工作。

2 个答案:

答案 0 :(得分:1)

您的错误很明显

2-22 14:18:59.448: W/System.err(1335): android.os.NetworkOnMainThreadException

它只是意味着您在主线程(UI线程)上进行网络调用。

使用AsynTask进行网络呼叫。请参阅此http://developer.android.com/reference/android/os/AsyncTask.html

另一个很好的例子是http://www.javasrilankansupport.com/2012/11/asynctask-android-example-asynctask-in.html

答案 1 :(得分:0)

可能会在execute方法中抛出异常。因此,下面执行的Log语句将不会被执行。

我看到你已经为Android编写了这段代码。检查您的MANIFEST文件中是否具有INTERNET权限。 添加一个catch块并查看异常堆栈跟踪。