0
pythonスクリプトでこのサーバーにリクエストを送信すると、私のアプリケーションにサーバー側が構築されますが、私のアプリケーションでは機能しません。[Android] -POST Json with HttpUrlConnection
サーバー:
サーバーにはデータベースが含まれています。 JSON {"name":"your_name"}
をサーバーに送信すると、DBに名前が保存されます。
のApp(アンドロイド):私はこのアプリを実行して、電話で
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSend=(Button)findViewById(R.id.button_send);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner postReq = new AsyncTaskRunner();
postReq.execute("start");
}
});
}
private class AsyncTaskRunner extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... params) {
try {
String url="my url";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestMethod("POST");
JSONObject cred = new JSONObject();
cred.put("name","my_name")
OutputStream os = con.getOutputStream();
os.write(cred.toString().getBytes("UTF-8"));
os.close();
}
catch (Exception e){
Log.v("ErrorAPP",e.toString());
}
return "";
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
私がデータを盗聴し、私はアプリがサーバにリクエストを送信していることがわかりますが、パケットは、JSONが含まれていません
:へ
:
変更すること。 OutputStream os = con.getOutputStream(); ObjectWriterライター= getMapper()。writer(); 文字列jsonObject = writer.writeValueAsString(要求); os.write(jsonObject); –
これをチェックしてください:http://stackoverflow.com/a/21405096/1263362 – Sayem
なぜjacksonですか?で、それ何?? – israelbenh