我是Android编程的新手。我想问一下如何在android native上发布json。这是我要发布的json样本。
{
"user_id":"88880005",
"keyword":"13828538",
"items":["99999999999999999996","99999999999999999997"]
}
这就是我所做的。
private void save() {
String URL = URL_Manager.URL_ScanIn;
Map<String, String> map = new HashMap<>();
map.put("user_id", id);
map.put("keyword", keyword);
map.put("items", databarcode);
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading");
progressDialog.setCancelable(false);
try {
aQuery.progress(progressDialog).ajax(URL, map, String.class, new AjaxCallback<String>() {
@Override
public void callback(String url, String object, AjaxStatus status) {
try {
JSONObject jsonObject = new JSONObject(object);
String result = jsonObject.getString("success");
String message= jsonObject.getString("message");
Log.d("tag", jsonObject.toString());
} catch (JSONException e) {
Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) { Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show();
}
}
在该样本中,我使用原始非邮政形式数据。我应该怎么做才能从我的android发布数据。
谢谢。
答案 0 :(得分:0)
以下是带有JSON请求的HTTP POST的示例代码。在AsyncTask或后台线程中使用它。
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public String executePost() {
String requestJSON = "your json";
InputStream inputStream = null;
String result = "";
try {
String myurl = "put your serverURL"
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(NetworkHelper.NetworkConnectTimeOut);
conn.setReadTimeout(12000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(requestJSON);
writer.flush();
writer.close();
os.close();
conn.connect();
int responseCode = conn.getResponseCode();
boolean responseStatus = false;
if (responseCode == HttpsURLConnection.HTTP_OK) {
responseStatus = true;
}
inputStream = conn.getInputStream();
if (inputStream != null && responseStatus) {
result = readStream(inputStream);
status = getJSONResponseStatus(result);
} else {
result = "Did not work!";
}
} catch (Exception e) {
Log.d(TAG, e.getMessage(), e);
}
Log.d("result****", result);
return result;
}
答案 1 :(得分:0)
在Android中按照以下步骤操作。在gradle中添加依赖
compile 'com.loopj.android:android-async-http:1.4.9'
使用以下代码调用您的网络服务
String[] items= new String[]{"99999999999","9999997456"};
JSONObject object=new JSONObject();
object.put("user_id",user_id);
object.put("keyword",keyword);
try {
JSONArray jsonArray = new JSONArray(Arrays.asList(items));
object.put("items",jsonArray);
}catch (Exception e){}
String yourData = object.toString();
StringEntity entity = null;
try {
entity = new StringEntity(yourData);
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
} catch(Exception e) {
//Exception
}
String url="Your url here";
new AsyncHttpClient().post(null,url,entity,"application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
try {
String object= new String(responseBody);
JSONObject jsonObject = new JSONObject(object);
String result = jsonObject.getString("success");
String message= jsonObject.getString("message");
Log.d("tag", jsonObject.toString());
} catch (JSONException e) {
Toast.makeText(getActivity(), e.getMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
}
});
}