0

我正在尝试从 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 - > _GET),但不是当我启动应用程序时......就像字符串“id_user”没有传递......我希望我很清楚。你能帮助我吗?

4

1 回答 1

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);
于 2012-12-06T15:32:12.320 回答