0

我一直在搜索如何从 mysql 中获取数据并使用自定义列表视图显示它们。现在,我发现 Http 请求有问题。谁能帮我检查这段代码?我不知道为什么我的代码不能工作。

MainActivity.java

public class MainActivity extends Activity {

    ArrayList<Mall> arrayOfMallData = new ArrayList<Mall>();

    class Mall{
        public String Mall_title;
        public String Mall_desc;
    }

    FancyAdapter fancyAdapter = null;
    static ArrayList<String> resultRow;


@Override
protected void onCreate(Bundle savedInstanceState) {

    try{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String result = "";

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/BasicPHP/getJSON.php");
            HttpResponse response = httpClient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();

            try {

                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }

                is.close();
                result = sb.toString();

            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try {

            JSONArray jArray = new JSONArray(result);

            for(int i=0;i<jArray.length();i++) {
                JSONObject f = jArray.getJSONObject(i);

                Mall resultRow = new Mall();

                resultRow.Mall_title = f.getString("Mall_Name");    
                resultRow.Mall_desc = f.getString("Mall_Business_Hours");

                arrayOfMallData.add(resultRow);              
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());     
        }


        ListView listView = (ListView)findViewById(R.id.listView);
        fancyAdapter = new FancyAdapter();
        listView.setAdapter(fancyAdapter); 

    } catch (Exception e) {
        Log.e("log_tag", "Error In Code: " + e.toString());
    }
}

class FancyAdapter extends ArrayAdapter<Mall> {
    FancyAdapter() {
        super(MainActivity.this, android.R.layout.simple_list_item_1, arrayOfMallData);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null){
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.malllist_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.populateFrom(arrayOfMallData.get(position));
        return(convertView);
    }
}

class ViewHolder {
    public TextView title = null;
    public TextView desc = null;

    ViewHolder(View row) {
        title = (TextView)row.findViewById(R.id.title);
        desc = (TextView)row.findViewById(R.id.desc);
    }

    void populateFrom(Mall r) {
        title.setText(r.Mall_title);
        desc.setText(r.Mall_desc);

    }
}  
}

这是我的 LogCat

05-16 15:36:53.334: I/dalvikvm(539): threadid=3: reacting to signal 3
05-16 15:36:53.384: I/dalvikvm(539): Wrote stack traces to '/data/anr/traces.txt'
05-16 15:36:53.494: E/log_tag(539): Error in http connection android.os.NetworkOnMainThreadException
05-16 15:36:53.494: E/log_tag(539): Error parsing data org.json.JSONException: End of input at character 0 of 
05-16 15:36:53.594: D/libEGL(539): loaded /system/lib/egl/libGLES_android.so
05-16 15:36:53.614: D/libEGL(539): loaded /system/lib/egl/libEGL_emulation.so
05-16 15:36:53.634: D/(539): HostConnection::get() New Host Connection established 0x1dee68, tid 539
05-16 15:36:53.654: D/libEGL(539): loaded /system/lib/egl/libGLESv1_CM_emulation.so
05-16 15:36:53.665: D/libEGL(539): loaded /system/lib/egl/libGLESv2_emulation.so
05-16 15:36:53.726: W/EGL_emulation(539): eglSurfaceAttrib not implemented
05-16 15:36:53.744: D/OpenGLRenderer(539): Enabling debug mode 0
05-16 15:36:53.854: I/dalvikvm(539): threadid=3: reacting to signal 3
05-16 15:36:53.875: I/dalvikvm(539): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

2

在主线程上的方法onCreate(Bundle savedInstanceState)中,您正在执行一些网络调用,这是问题的原因,您不允许HttpResponse response = httpClient.execute(httppost);在主线程上执行此操作。将网络部分移动到不同的线程上。看看这个AsyncTask Android 示例

于 2013-05-16T08:57:49.223 回答
0
android.os.NetworkOnMainThreadException 

当应用程序尝试在其主线程上执行网络操作时,将引发此异常。在 AsyncTask 中运行您的代码。喜欢

private class RetriveJson extends AsyncTask<String, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(String... params) {
                   //retrive your data via url from here
        }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json;
    }

}

并在您想要的位置调用此任务

new RetriveJson().execute("你的网址");

我希望这能帮到您。

于 2013-05-16T09:05:11.920 回答