0

我正在尝试构建一个 POST 数据包并将其传递到仅包含数据的某个 url:

value={ "y": 2102 }

所以我尝试创建以下类:

class myPostReq {
    HttpResponse response = null;
    StringEntity tmp = null;
    HttpPost httpPost = null;
    DefaultHttpClient httpClient;
    HttpContext localContext;

    public myPostReq() { }
    public int post(String url,String data) {
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);
        localContext = new BasicHttpContext(); 
        int ret = 0;
        try {
            tmp = new StringEntity("value={ y: " + data + "}","UTF-8");
        } catch (UnsupportedEncodingException e) {
            ret = -1;
        }
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setEntity(tmp);
        try {
            response = httpClient.execute(httpPost,localContext);

            if (response != null) {
                ret = response.getStatusLine().getStatusCode();
            }
        } catch (Exception e) {
            ret = -2;
        }
        return ret;
    }
}

但是,当我执行 post(..) 方法时,它一直在崩溃。我不明白我做错了什么。

编辑:提供Logcat:

02-08 18:21:51.841: W/System.err(2034): android.os.NetworkOnMainThreadException
02-08 18:21:51.851: W/System.err(2034):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-08 18:21:51.851: W/System.err(2034):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-08 18:21:51.851: W/System.err(2034):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-08 18:21:51.861: W/System.err(2034):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-08 18:21:51.861: W/System.err(2034):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-08 18:21:51.871: W/System.err(2034):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-08 18:21:51.871: W/System.err(2034):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-08 18:21:51.871: W/System.err(2034):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-08 18:21:51.881: W/System.err(2034):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-08 18:21:51.881: W/System.err(2034):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-08 18:21:51.881: W/System.err(2034):     at com.example.remoteswitch.myPostReq.post(MainActivity.java:266)
02-08 18:21:51.891: W/System.err(2034):     at com.example.remoteswitch.MainActivity.startOperation(MainActivity.java:198)
02-08 18:21:51.891: W/System.err(2034):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:21:51.891: W/System.err(2034):     at java.lang.reflect.Method.invoke(Method.java:511)
02-08 18:21:51.891: W/System.err(2034):     at android.view.View$1.onClick(View.java:3592)
02-08 18:21:51.901: W/System.err(2034):     at android.view.View.performClick(View.java:4202)
02-08 18:21:51.901: W/System.err(2034):     at android.view.View$PerformClick.run(View.java:17340)
02-08 18:21:51.901: W/System.err(2034):     at android.os.Handler.handleCallback(Handler.java:725)
02-08 18:21:51.911: W/System.err(2034):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 18:21:51.911: W/System.err(2034):     at android.os.Looper.loop(Looper.java:137)
02-08 18:21:51.911: W/System.err(2034):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-08 18:21:51.921: W/System.err(2034):     at java.lang.reflect.Method.invokeNative(Native Method)
02-08 18:21:51.921: W/System.err(2034):     at java.lang.reflect.Method.invoke(Method.java:511)
02-08 18:21:51.941: W/System.err(2034):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-08 18:21:51.941: W/System.err(2034):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-08 18:21:51.941: W/System.err(2034):     at dalvik.system.NativeStart.main(Native Method)
02-08 18:21:52.080: I/Choreographer(2034): Skipped 66 frames!  The application may be doing too much work on its main thread.
02-08 18:21:52.080: W/Trace(2034): Unexpected value from nativeGetEnabledTags: 0
4

2 回答 2

1

必须httpPost在引用andhttpClient的地方获得 NullPointerException localContext。您应该将它们初始化为...

httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
// localContext = HttpSupport.getHttpContextInstance(); ???

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("value", "{ y: 2102 }"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

在引用它们之前。您还应该添加代码以检查您response喜欢的返回代码...

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
   // notify user
}
于 2013-02-08T16:30:46.577 回答
0

看来您既没有初始化 httpPost,也没有设置 requestURL。尝试这个...

defaultHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(requestUrl);
httpResponse = defaultHttpClient.execute(httpPost);
于 2013-02-08T16:32:00.517 回答