2017-03-06 18 views
0

私は、私が持っている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(); 
} 

答えて

0

以下のコードを試してください:

URL url = new URL(YOUR_URL); 
try { 
    HttpURLConnection myURLConnection = (HttpURLConnection) url.openConnection(); 
    myURLConnection.setRequestMethod("POST"); 
    myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    myURLConnection.setRequestProperty("os-name", Build.VERSION.RELEASE); // 6.x.x 
    myURLConnection.setDoInput(true); 
    myURLConnection.setDoOutput(true); 

} catch (IOException e) { 
    e.printStackTrace(); 
} 

をあなたがのsetRequestProperty(String型のキー、文字列値)によって、他のプロパティを設定することがあります。

+0

私は、アプリケーションのos名をフィールドとしてサーバーに送信する場合は、どのキーをsetrequestproperty()メソッドで使用する必要がありますか? –

+0

os name?サーバーに送信するカスタムデータですか? – Alan

+0

yaa!実際に私はデータのユーザー名とパスワードと共にサーバーにOS名を送信する必要があります。 os名はヘッダーに送られます。どうやってするか? –

0

あなたがいる場合アンドロイドの新機能、標準のPOSTが必要作業するメカニズム、試してくださいRetrofit。そのスクエアであり、世界中で信頼されています。

初心者の方であれば、間違いが起きやすくなりますが、同じライブラリを使用すると、すべてのボイラープレートコードを書き留めてバグの可能性を減らすことができます。

+0

実際には、このタスクを行うためにヘッダーを使用するように割り当てられています。したがって、改造を使用することはできません。あなたはそれのためのコードを教えてくださいできますか? –

0

HttpURLConnectionを使用してPOSTデータをWebサーバーに送信する場合、このexampleが見つかりました。

+0

この例では、ヘッダー経由でデータを送信する方法については説明していません。あなたは説明できますか? –

+0

'connection =(HttpURLConnection)url.openConnection();connection.setRequestMethod( "POST"); connection.setRequestProperty( "Content-Type"、 "application/x-www-form-urlencoded"); ' HttpURLConnectionのsetRequestPropertyメソッドは、POSTメソッドのヘッダーを設定します。 –

関連する問題