Android:MySQL数据传递失败

时间:2012-12-06 15:08:05

标签: android mysql

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

那是.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 - &gt; _GET),但是当我启动应用程序时却没有...就像没有传递字符串“id_user”... 我希望我很清楚。你能救我吗?

1 个答案:

答案 0 :(得分: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);