2012-12-06 51 views
0

我试图从MySQL DB中获取一些数据并在文本视图中打印它。 我正在处理一个查询,该查询使用我的应用程序通过_POST传递的数据来识别行。Android:MySQL数据传递失败

那是的.java:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_calendario); 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 

    app_preferences = PreferenceManager.getDefaultSharedPreferences(this); 

    LoginClass ID_P = ((LoginClass)getApplicationContext()); 
    id_user = ID_P.getID(); 
    Log.d("id_w?",id_user); 


    text = (TextView) findViewById(R.id.text_pren); 
    fill_text(); 

} 

private void fill_text(){ 

    try{ 
     httpclient = new DefaultHttpClient(); 
     httppost = new HttpPost("******MY URI*******"); 
     response = httpclient.execute(httppost); 
     prenotazione = new ArrayList<NameValuePair>(1); 
     prenotazione.add(new BasicNameValuePair("id_user",id_user)); 
     httppost.setEntity(new UrlEncodedFormEntity(prenotazione)); 
     Log.d("gladdi",prenotazione.toString()); 
     HttpEntity entity = response.getEntity(); 
     inputStream = entity.getContent(); 


     } 
     catch (Exception e){ 
      Toast.makeText(CalendarioActivity.this, "error"+e.toString(), Toast.LENGTH_LONG).show(); 
     } 
      if(inputStream != null){ 
      Log.d("glad2",id_user); 
      try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       inputStream.close(); 
       result = sb.toString(); 

       Log.d("sort",result); 
      }catch(Exception e){ 
       Log.e("TEST", "Errore nel convertire il risultato "+e.toString()); 
       } 
     try{ 
      JSONArray jArray = new JSONArray(result); 
      prenotazioni = ""; 
      for(int i=0;i<jArray.length();i++){ 
       JSONObject json_pren = jArray.getJSONObject(i); 
       result_string = "Prenotazione in data "+json_pren.getString("data")+", "+json_pren.getString("dettagli")+"\n\n"; 
       prenotazioni = prenotazioni + result_string; 
       } 
      }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
        } 
     }else{ 
      //do nothing 
     } 

      text.setText(prenotazioni); 
}//end fill_text() 

....

如果我用它工作的浏览器(更换_POST - > _GET)查询,而不是当我启动应用程序。 ..这就像字符串“id_user”没有通过... 我希望我很清楚。你可以帮我吗?

回答

0

我相信这只是一个疏忽,但它有助于发送参数之前,你得到的结果。

更改此:

response = httpclient.execute(httppost); 
    prenotazione = new ArrayList<NameValuePair>(1); 
    prenotazione.add(new BasicNameValuePair("id_user",id_user)); 
    httppost.setEntity(new UrlEncodedFormEntity(prenotazione)); 

这样:

prenotazione = new ArrayList<NameValuePair>(1); 
    prenotazione.add(new BasicNameValuePair("id_user",id_user)); 
    httppost.setEntity(new UrlEncodedFormEntity(prenotazione)); 
    response = httpclient.execute(httppost); 
+0

哦,多么巨大的不小心!谢谢朋友! – MikeKeepsOnShine

+0

这一切都很好。发生。别客气。 – 323go