私は、私が持っているWebサービスapiを使用してサーバーにヘッダー経由でos-nameとバージョン番号を送信する必要がある、Androidの簡単なログインフォームを作成しています。私はアンドロイドの初心者ですが、addHeaderなどのメソッドは廃止されました。誰か助けてもらえますか? ここに私のコードはありますか:アンドロイドのWebサーバーへのヘッダーのPOSTデータ?
ログイン:サーバーのヘッダーにos_ os_versionフィールドをPOSTしたいと思います。どうやってやるの?以下は私のコード
String user = editText_name.getText().toString();
String pass =editText_pass.getText().toString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("user_name",user);
jsonObject.put("password",pass);
String url = WsUtils.BASE_URL+WsUtils.WS_LOGIN;
if (jsonObject.length() > 0) {
new sendJsonData().execute(getPostDataString(jsonObject),url);
}else Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show();
sendJsonData():: >>
public class sendJsonData extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
String Jsonresponse = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String Jsondata = params[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[1]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(10000);
urlConnection.setRequestProperty("Accept","application/json");
//urlConnection.setRequestProperty("Content-Type","application/json");
OutputStream out = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(Jsondata);
writer.flush();
writer.close();
out.close();
Log.e("json", Jsondata);
DataInputStream in = new DataInputStream(urlConnection.getInputStream());
Jsonresponse = convertStreamToString(in);
return Jsonresponse;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
Log.e("response", Jsonresponse);
}
}
getPostDataStringは()である - > `
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
私は、アプリケーションのos名をフィールドとしてサーバーに送信する場合は、どのキーをsetrequestproperty()メソッドで使用する必要がありますか? –
os name?サーバーに送信するカスタムデータですか? – Alan
yaa!実際に私はデータのユーザー名とパスワードと共にサーバーにOS名を送信する必要があります。 os名はヘッダーに送られます。どうやってするか? –