源附件不包含DefaultRequestDirector.class Android

时间:2013-05-15 12:34:39

标签: android json apache httpclient httpresponse

我是Android上的新手,我正在开发一个连接php页面的应用程序。但是,当我调试我的代码时,它会提供Source not found上次为DefaulRequestDirector.class提供的代码;但是,它也为Abstracthttpclient.class提供了它。我导入了jsonapache个包。

我在Source not found行看到HttpResponse httpResponse = httpClient.execute(httpPost);

 if(method == "POST")
            {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost); // Source not found
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }

logcat的:

05-15 16:15:46.133: W/dalvikvm(25727): threadid=1: thread exiting with uncaught exception (group=0x415262a0)
05-15 16:15:46.138: E/AndroidRuntime(25727): FATAL EXCEPTION: main
05-15 16:15:46.138: E/AndroidRuntime(25727): android.os.NetworkOnMainThreadException
05-15 16:15:46.138: E/AndroidRuntime(25727):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at com.example.yoritreader.JSONParser.makeHttpRequest(JSONParser.java:59)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at com.example.yoritreader.MainActivity$1.onPreviewFrame(MainActivity.java:246)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:841)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at android.os.Looper.loop(Looper.java:137)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at android.app.ActivityThread.main(ActivityThread.java:4921)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at java.lang.reflect.Method.invokeNative(Native Method)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at java.lang.reflect.Method.invoke(Method.java:511)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
05-15 16:15:46.138: E/AndroidRuntime(25727):    at dalvik.system.NativeStart.main(Native Method)

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

android.os.NetworkOnMainThreadException

你得到了NetworkOnMainThreadException。这意味着您正在UI THread上执行阻止操作,例如涉及网络的操作。这种行为一直被深深地贬低,但自从Android 3.0以来一直被禁止。所以你应该在AsyncTask上移动你的代码。

此外,在Java中,无法使用==运算符执行字符串比较。

if(method == "POST")
这是错的。你应该使用

string1.equals(string2)

string1.equalsIgnoreCase(string2)

您可以阅读此guide for Thread / AsyncTask