Android中的JSON Post问题

时间:2015-05-04 07:01:34

标签: android json post

{
     "AppId":"222",
      "AdminUserId":"118",
      "Data": [
       {"FormId":456,
      "FormFieldList":[
      {"Value":"owe app","FieldId":"1727"},       
      {"Value":"","FieldId":"1728"}]}
    ],
    "key":"22"

}

我试图将以上json数据作为参数'jsonData'发布在以下url中: http://apptudio.com/api/OWE/SaveAllDataByForm

这就是我尝试这样做的方式:

JSONObject jsonParams = new JSONObject();
StringEntity entity = null;
String jsonFormData = "{\"AppId\":\"222\",\n" +
                "\"AdminUserId\":\"118\", \"key\":\"22\",  \"Data\":[  { \"FormId\":456,\"FormFieldList\":[ {\"Value\":\"shadek\",\"FieldId\":\"1727\"},{\"Value\":\"\",\"FieldId\":\"1728\"}] }  ]  }";
try {
  jsonParams.put("jsonData", jsonFormData);
  try {
    entity = new StringEntity(jsonParams.toString());

  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }

} catch (JSONException e) {
  e.printStackTrace();
}

AsyncHttpClient client = new AsyncHttpClient();
client.post(mContext, url, entity, "application/json", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.d("Success.....", new String(responseBody));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                if(responseBody != null)
                Log.d("Failure......", new String(responseBody));
                else
                    Log.d("Failure....", ".............");
            }
        });

我在android中获得了以下调试结果:

D/Failure......﹕ [ 05-04 11:14:03.213    91:  272 D/AudioHardware ]
    AudioHardware pcm playback is going to standby.

我是否从服务器收到此失败消息?或者没有请求服务器?

1 个答案:

答案 0 :(得分:0)

我们有点网服务器而不是php服务器。这可能是不能正常工作的原因。后来我通过以下方式解决了这个问题:



final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

NameValuePair params = new BasicNameValuePair("jsonData",jsonFormData);
params.add(nameValuePair);

DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpPost httpPost = new HttpPost(url);

String str = new UrlEncodedFormEntity(params).toString();

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

InputStream is = null;

BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
  sb.append(line + "\n");
}
is.close();
Log.d("response", sb.toString());
return sb.toString();
&#13;
&#13;
&#13;