2016-07-06 14 views
0

我想使用這個網址www.example.com/directory/來從服務器的數據,但它不工作...它想出了這一點,JSON不發送到服務器的任何行動......所以我要的是使URL這方式 - www.example.com/directory/index.php?action=searchFood &錢= 50如何進行參數的URL在Android的JSON

這是我的JsonFetcher.java

public class JsonFetcher extends AsyncTask<Pair<String, String>, Integer, JSONObject> { 

private JSONObject jsonObject; 
public AsyncResponse delegate = null; 

public interface AsyncResponse { 
    void processFinish(JSONObject output); 
} 

public JsonFetcher(AsyncResponse delegate) { 
    this.delegate = delegate; 
} 

@Override 
protected void onPostExecute(JSONObject jsonObject) { 
    super.onPostExecute(jsonObject); 
    delegate.processFinish(jsonObject); 
} 

@Override 
protected JSONObject doInBackground(final Pair<String, String>... params) { 

    int count = params.length; 
    URL url = null; 
    int responseCode = 0; 
    jsonObject = null; 
    try { 
     url = new URL("http://www.example.com/directory/"); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    HttpURLConnection conn = null; 
    try { 
     conn = (HttpURLConnection) url.openConnection(); 
     //conn.connect(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     Uri.Builder builder = new Uri.Builder(); 
     Date date = new Date(); 
     String md5 = date.toString(); 

     //Log.e("json lock",lock); 
     //Log.e("json key",md5); 

     for (int i = 0; i < count; i++) { 
      builder.appendQueryParameter(String.valueOf(params[i].first),String.valueOf(params[i].second)); 
     } 
     String query = builder.build().getEncodedQuery(); 
     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
     writer.write(query); 
     writer.flush(); 
     writer.close(); 
     os.close(); 
    } catch (ProtocolException e) { 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // read the response 
    try { 
     responseCode = conn.getResponseCode(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (responseCode == HttpsURLConnection.HTTP_OK) { 
     InputStream in = null; 
     try { 
      in = new BufferedInputStream(conn.getInputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      //conn.disconnect(); 
     } 

     BufferedReader r = new BufferedReader(new InputStreamReader(in)); 
     StringBuilder total = new StringBuilder(); 
     String line; 
     try { 
      while ((line = r.readLine()) != null) { 
       total.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     Log.e("json", total.toString()); 
     try { 
      jsonObject = new JSONObject(total.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return jsonObject; 
} 

}

是否有可能以這種方式來寫url = new URL("http://www.example/directory/index.php?action= PairActionName & PairMoney");這裏PairActionName和PairMoney將從MainActivity

+0

在任何REST客戶端上都嘗試了這種方法嗎?你在REST客戶端上獲得什麼? –

+0

先生,當我嘗試在xampp上使用它得到的結果,並顯示在這個url的列表視圖(「http://192.168.1.110/directory/」) –

+0

在服務器上傳後我得到問題 –

回答

0

接收你正在使用的AsyncTask執行服務器操作,因爲,你要創建一個編碼的查詢字符串將參數傳遞給url,並將其寫入URLconnection對象。下面的例子將清除剛纔說的:

Uri.Builder builder = new Uri.Builder().appendQueryParameter("param1", value1) .appendQueryParameter("param2", value2); final String query = builder.build().getEncodedQuery(); URL url; HttpURLConnection connection = null; try{ url = new URL("http://www.example.com/directory/"); connection = (HttpURLConnection) url.openConnection(); OutputStream os = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(query); writer.flush(); writer.close(); os.close(); connection.connect(); }

在服務器端,你可以簡單地檢索使用參數名稱(參數1)參數值(值)。 另外,您可以使用.appendQueryParameter()方法添加儘可能多的參數。

+0

請投票選出答案 – myrahKaur

相關問題