2015-11-04 147 views
-2

喜的朋友,请帮助我,我已经从15天击中我不能够找到我已经到处找那就没办法了。 这是代码我使用使http请求,其中它的传递参数在JSON 在此下面字符串其传递空字符串到jurl String jurl=calling.makeHttpRequest(url, "GET", params);HttpPost使用make http请求

public class loaditems extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... strings) { 
     String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway"; 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("cname", item)); 
     params.add(new BasicNameValuePair("cids",ids)); 
     Calling calling=new Calling(); 
     String jurl=calling.makeHttpRequest(url, "GET", params); 
     Log.d("items", jurl); 

     try { 
      JSONArray array = new JSONArray(jurl); 
      for (int i = 0; i < array.length(); i++) { 
       JSONObject first = array.getJSONObject(i); 
       JSONParser parser = new JSONParser(); 
       parser.setMenuname(first.getString("menu_name")); 
       JSONArray getitems = first.getJSONArray("items"); 
       for (int j = 0; j < getitems.length(); j++) { 
        JSONObject sitems = getitems.getJSONObject(j); 
        parser.setIid(sitems.getInt("id")); 
        parser.setBaseName(sitems.getString("BaseName")); 
        parser.setItemdesc(sitems.getString("itemdesc")); 
        JSONArray subitems = sitems.getJSONArray("subitems"); 
        for (int l = 0; l < subitems.length(); l++) { 
         JSONObject thrid = subitems.getJSONObject(l); 
         parser.setSid(thrid.getInt("id")); 
         parser.setSubItemdesc(thrid.getString("SubItemdesc")); 
         parser.setSubItemprice(thrid.getString("SubItemprice")); 
        } 
        itemsdata.add(parser); 
       } 
       secondAdapter.notifyDataSetChanged(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     Toast.makeText(getApplicationContext(),"Data Loaded", Toast.LENGTH_SHORT).show(); 
     TextView textView= (TextView) findViewById(R.id.textView); 
     textView.setText(item); 
    } 
} 

Calling.Class 公共类呼叫{

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public Calling() { 

} 

// function get json from url 
// by making HTTP POST or GET mehtod 
public String makeHttpRequest(String url, String method, 
           List<NameValuePair> params) { 

    // Making HTTP request 
    try { 

     // check for request method 
     if (method == "POST") { 
      // request method is POST 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } else if (method == "GET") { 
      // request method is GET 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     } 

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

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // return JSON String 
    return json; 

} 

}

+0

使用凌空LIB ..... –

回答

1
See the code below 

// request method is GET 

DefaultHttpClient httpClient = new DefaultHttpClient(); 
String paramString = URLEncodedUtils.format(params, "utf-8"); 
url += "?" + paramString; 
HttpGet httpGet = new HttpGet(url); 
HttpResponse httpResponse = httpClient.execute(httpGet); 
HttpEntity httpEntity = httpResponse.getEntity(); 
is = httpEntity.getContent(); 




line url += "?" + paramString; 


adding a query string separator '?' in your url. 
but in a valid url only query string separator '?' is allowed but you already include that one in your url 

Try this solution 
change your url from 
String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway"; 

to`enter code here` 

String url="http://www.yell4food.com/json/data_standard_item_new.php"; 

and pass parameter rname in NameValuePairs list like this 

    params.add(new BasicNameValuePair("rname", "standardtakeaway")); 
+0

我已经编辑了问题补充调用的类中一次检查也请 –

+0

我查看调用的类。我编辑我的答案 –

+0

LOVE U老兄它已经解决了我的问题,但数据以不同的方式来兄弟我曾试图直到近15天兄弟 –