PDA

View Full Version : مشکل در ارسال JSON



mehdi.safavie
چهارشنبه 18 آذر 1394, 00:39 صبح
درود;

من میخوام این دستور رو :




"{\"AAS1\": \"your_AAS1\",\"AAS2\": \"AAS2\",\"AAS3\": 310,\"AAS4\": 410,\"AAS5\": [{\"AAS51\": 7033,\"AAS52\": 17811}],\"AAS6\": [{\"AAS7\": \"00:17:c5:cd:ca:aa\",\"AAS8\": 11,\"AAS9\": 2412,\"AAS10\": -51}, {\"AAS7\": \"d8:97:ba:c2:f0:5a\"}],\"AAS11\": 1}"


به این آدرس ارسال کنم :
https://xxxxxxxxxx.com/xx/process.php

اگر راه حل بهتری دارید لطفا در جریان بگذارید تا از این راه نرم دیگه .

از این کد برای ارسال JSON استفاده کردم :

class AsyncT extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://ap1.unwiredlabs.com/v2/process.php");

try {





JSONObject jsonobj = new JSONObject();

jsonobj.put("AAS1", "myAAS1");
jsonobj.put("AAS2", "AAS2");
jsonobj.put("AAS3", 432);
jsonobj.put("AAS4", 11);
JSONObject AAS5 = new JSONObject();
AAS5.put("AAS51", 31131);
AAS5.put("AAS52", 3415);
JSONArray jsonArray = new JSONArray();
jsonArray.put(Cells);
jsonobj.put("AAS5", jsonArray);
JSONObject AAS6= new JSONObject();
AAS6.put("AAS7", "00:17:c5:cd:ca:aa");
AAS6.put("AAS8", 11);
AAS6.put("AAS9", 2412);
AAS6.put("AAS10", -51);
JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(Wifi);
jsonobj.put("AAS6", jsonArray2);
jsonobj.put("AAS7", "d8:97:ba:c2:f0:5a");
jsonobj.put("AAS11", 1);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("rep", jsonobj.toString()));

Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());

// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
InputStream inputStream = response.getEntity().getContent();
InputStreamToStringExample str = new InputStreamToStringExample();
responseServer = str.getStringFromInputStream(inputStream);
Log.e("response", "response -----" + responseServer);

javab = responseServer;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(context,responseServer,Toast.LENGTH _LONG).show();
}
}

public static class InputStreamToStringExample {

public static void main(String[] args) throws IOException {

// intilize an InputStream
InputStream is =
new ByteArrayInputStream("file content..blah blah".getBytes());

String result = getStringFromInputStream(is);

System.out.println(result);
System.out.println("Done");

}

// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {

BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {

br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}

}

اما مقداری که تو responseServer بر میگردونه مقدار Error هست .
کجای کارم دارم اشتباه میرم ؟