连接到数据库错误

时间:2014-12-10 13:02:29

标签: android

我正在尝试显示数据库中的项目,但它只是给了我Failure吐司......

这些行主要来自教程,我只是稍微修改了一下以适应我的数据库。我真的不知道什么是错的。

任何帮助将不胜感激......

以下是代码:

package com.example.eksapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class Message extends Activity {

    private JSONObject jObject; 
    private String xResult = "";    
    private String url = "http://10.0.2.2/testing/get_message.php";
    Button list;    

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

        list=(Button)findViewById(R.id.list_message);
        list.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v){

                //going to asynctask...
                new getting().execute();
            }
        });
        }

    private class getting extends AsyncTask <Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... arg0) {

            TextView txtResult = (TextView)findViewById(R.id.field_message);
            xResult = getRequest(url);
            try{
                parse(txtResult);           
            } catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }

    private void parse(TextView txtResult) throws Exception{
        jObject = new JSONObject(xResult);

        JSONArray menuitemArray = jObject.getJSONArray("message");
        String sret = "";
        for (int i = 0; i < menuitemArray.length(); i++){
            sret +=menuitemArray.getJSONObject(i).getString("username").toString()+" : ";

            System.out.println(menuitemArray.getJSONObject(i)
                .getString("username").toString());

            sret +=menuitemArray.getJSONObject(i)
                .getString("subject").toString()+"\n";
        }
        txtResult.setText(sret);
    }

    public String getRequest (String Url){

        String sret = "";
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(Url);
        try{
            HttpResponse response = client.execute(request);
            sret = request(response);

        }catch(Exception ex){
            Toast.makeText(this,"Failure "+sret, Toast.LENGTH_SHORT).show();
        }
        return sret;
    }

    public static String request(HttpResponse response){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }catch(Exception ex){
            result = "Error";
        }
        return result;
        }

}

现在问题是,数据库中的项目显示在logcat中,而不是field_message TextView ... Logcat:

12-10 09:37:07.770: I/System.out(2671): marketing
12-10 09:37:07.770: I/System.out(2671): Testing 2
12-10 09:37:07.770: I/System.out(2671): marketing
12-10 09:37:07.770: I/System.out(2671): Testing

1 个答案:

答案 0 :(得分:1)

您正在主线程上进行联网,这在Android since Honeycomb中是不允许的。使用AsyncTask在后台执行此操作,您应该没问题。

此处有更多信息:http://developer.android.com/training/articles/perf-anr.html