如何调用并获取json webservice?

时间:2015-07-30 02:26:55

标签: android json httpresponse

我有一个问题,就是当我尝试获得响应时,我的代码被停止了。

public String JCon() {

    StringBuilder strcon = new StringBuilder();
    strcon.append("{");
    strcon.append(" \"username\":\"aaaaaaaa\",");
    strcon.append("\"password\":\"1\",");
    strcon.append("\"device_type\":\"ANDROID\",");
    strcon.append("\"device_token\":\"1234567890\"");
    strcon.append("}");


    HttpClient httpC = new DefaultHttpClient();
    HttpPost httpP = new HttpPost(
            "http://203.162.10.112:8080/ezcheck/rest/user/login");
    JSONObject jcon;
    try {
        jcon = new JSONObject(strcon.toString());
        httpP.setEntity(new StringEntity(jcon.toString()));

        // error in here
        HttpResponse httpR = httpC.execute(httpP);

    } catch (JSONException e1) {
        return "Can't creat Json";
    } catch (UnsupportedEncodingException e) {
        return "UnsupportedEncodingException";
    } catch (ClientProtocolException e) {
        return "ClientProtocolException";
    } catch (IOException e) {
        return "IOException";
    }
}

这是我尝试测试连接并从webservice获取响应的代码。我无法得到任何异常,所以我无法理解什么是问题,我只知道我的问题是在HttpResponse httpR = httpC.execute(httpP);

2 个答案:

答案 0 :(得分:1)

您无法在主线程上打开http或其他网络连接,这会导致致命异常NetworkOnMainThreadException。 如果您在IDE中无法观察到任何异常,请打开command line并输入:

  

adb logcat" *:E"

您会看到异常。

在您的代码中尝试此操作:

new Thread(new Runnable(){
    JCon();
});

然后就可以了,但我建议使用HandlerAsynctask代替Thread

答案 1 :(得分:0)

您可以使用我的代码:

pd.concat

AND ...使用你需要这样做的方法:

np.empty(... dtype)

你将在日志猫中看到它。

相关问题