2015-05-04 137 views
-2

在android系统

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

 
}

我想在下面的网址张贴上述JSON数据作为参数“jsonData” JSON帖子的问题: 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.

难道我从服务器获取该故障消息?或者服务器没有被请求?

+0

什么'jsonFormData':后来我通过执行以下操作来解决呢? – greenapps

+0

你也应该记录两个句柄的状态码。请告诉。 – greenapps

+0

jsonFormData是第一个代码片段。 –

回答

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();