2017-03-25 4 views
-1

私のポストサービスでキー値ペアを使用しています。私は「無効なパラメータ」として応答を得ています。私は同じ種類の記事を「application/x-www-form-urlencoded」というコンテンツタイプを使って投稿してみましたが、うまくいき、期待どおりのレスポンスを得ました。私は以下?何をしないのです、私はアンドロイドのキー値ペアを使用してポストサービスを使用しているときに無効なパラメータエラーが発生しました

private String invokeWebservice() { 
    String data = null; 
    HttpURLConnection conn = null; 
    BufferedReader in = null; 
    try { 
     String webservice = Constants.BASE_URL + serviceName; 

     URL url = new URL(webservice); 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(10000); 
     conn.setConnectTimeout(15000); 
     conn.setUseCaches(false); 

     if (isPost) { 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      conn.setDoOutput(true); 

      Writer writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); 
      if (jsonObject != null) 
       writer.write(jsonObject.toString()); 
      else { 
       writer.write(getPostDataString(keyValue)); 
      } 
      writer.close(); 
     } 

     in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     StringBuffer sb = new StringBuffer(""); 
     String l = ""; 
     String nl = System.getProperty("line.separator"); 
     while ((l = in.readLine()) != null) 
      sb.append(l + nl); 
     in.close(); 

     data = sb.toString(); 

     return data; 
    } catch (Exception e) { 
     Log.e("test", e.getMessage()); 
    } finally { 
     try { 
      if (conn != null) 
       conn.disconnect(); 
      if (in != null) 
       in.close(); 
     } catch (Exception ex) { 

     } 
    } 

    return data; 
} 

を使用していたコードであり、私はあなたがこのアプローチを使用してくださいリクエストにパラメータを追加しないURLに

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder postData = new StringBuilder(); 
    for (Map.Entry<String, String> entry : params.entrySet()) { 
     if (postData.length() != 0) 
      postData.append('&'); 
     postData.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     postData.append('='); 
     postData.append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8")); 
    } 
    return postData.toString(); 
} 

答えて

0

をパラメータを追加するためのコードの下に使用しています。

JsonParserクラスを作成します。それは

JSONPARSER.java今、あなたのMainActivityで

package com.example; 

import android.content.Context; 
import android.os.Handler; 
import android.os.Looper; 
import android.util.Log; 
import android.widget.Toast; 

import org.json.JSONObject; 

import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

/** 
* Created by Ghanshyam on 9/18/2014. 
*/ 
public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    HttpURLConnection urlConnection = null; 
    // variable to hold context 
    private Context context; 
    // constructor 
    public JSONParser(Context context){ 
     this.context=context; 
    } 


    public JSONObject makeHttpRequest(String url,String method,String params) { 

     // boolean isReachable =Config.isURLReachable(context); 
     // Making HTTP request 
     try { 
      String retSrc=""; 
      char current = '0'; 

       URL url1 = new URL(url); 
       // check for request method 
       HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection(); 
      if (method == "POST") { 
       // request method is POST 
       urlConnection.setDoOutput(true); 
       urlConnection.setRequestMethod("POST"); 
       urlConnection.setFixedLengthStreamingMode(params.getBytes().length); 
       urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       PrintWriter out = new PrintWriter(urlConnection.getOutputStream()); 
       out.print(params); 
       out.close(); 
      } 
       InputStream in = urlConnection.getInputStream(); 

       InputStreamReader isw = new InputStreamReader(in); 

       byte[] bytes = new byte[10000]; 
       StringBuilder x = new StringBuilder(); 
       int numRead = 0; 
       while ((numRead = in.read(bytes)) >= 0) { 
        x.append(new String(bytes, 0, numRead)); 
       } 
       retSrc=x.toString(); 



      jObj = new JSONObject(retSrc); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      new Handler(Looper.getMainLooper()).post(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(context, "Connectivity issue. Please try again later.", Toast.LENGTH_LONG).show(); 
       } 
      }); 
      return null; 
     }finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
      } 
     return jObj; 
    } 
} 

はdoInBackgroundメソッドでサーバ要求が

 private class Sample extends AsyncTask<String,String,String> { 
      ProgressDialog progressDialog; 

      String status; 
      String param; 

      @Override 
      protected void onPreExecute() { 
       progressDialog=new ProgressDialog(MainActivity.this); 
       progressDialog.setMessage("Please wait..."); 
       progressDialog.setCancelable(false); 
       progressDialog.setIndeterminate(false); 
       progressDialog.show(); 
       super.onPreExecute(); 
      } 
     @Override 
      protected String doInBackground(String... args) { 
String webservice = Constants.BASE_URL + serviceName; 

        JSONParser jsonParser = new JSONParser(MainActivity.this); 
        try { 
         param="hash=" + URLEncoder.encode(email,"UTF-8")+ 
         "&key="+URLEncoder.encode(password,"UTF-8"); 
        } catch (UnsupportedEncodingException e) { 
         e.printStackTrace(); 
        } 
        JSONObject jsonObject = jsonParser.makeHttpRequest(webservice ,"POST",param); 
        if(jsonObject != null){ 
         //code to get data 
          } 


         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
        return null; 
       } 

       @Override 
       protected void onPostExecute(String s) { 
        super.onPostExecute(s); 

       } 
      } 
    } 
+0

それを使用してください作る名前とそれが@Abdul Waheedを動作していないなら、私に知らせましょう –

関連する問題