我已经创建了一个Android应用程序,因为我使用此代码从服务器接收json对象,我需要从android发送来自同一个android活动的json对象我该怎么办?强文 < / p>
class GetDataJSON extends AsyncTask<String, Void, String> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(anscheck.this, "Please wait",
"Loading...");
}
@Override
protected String doInBackground(String... params) {
InputStream inputStream = null;
String result = null;
try {
DefaultHttpClient httpclient = new DefaultHttpClient(
new BasicHttpParams());
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
loadingDialog.dismiss();
String s = result.trim();
{
myJSON = result;
showList();
}
}
}
答案 0 :(得分:0)
您可以创建JSONObject
您需要发布到服务器的任何数据,并将其设置为StringEntity
到您的`HttpPost&#39;方法调用,
下面我提供一些参考代码,
首先创建您需要发布的JSON数据,例如
public String loginArray() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("email", uName);
jsonObject.put("password", password);
} catch (JSONException exception) {
}
UiUtils.showLog(jsonObject.toString());
return jsonObject.toString();
}
现在将其设为StringEntity
HttpPost
即
httppost.setEntity(new StringEntity(loginArray())
在设置header