我无法在我的Android应用程序中从mysql检索数据

时间:2014-03-17 09:33:24

标签: android mysql json

我是Android应用程序开发的新手。我正在开发一个将从mysql数据库中检索新闻的应用程序。我首先编写PHP脚本,从数据库中提取新闻标题和内容,并成功输出JSON编码数据,然后在我的应用程序中,我编写了以下代码,将采用JSON编码数据并将其显示给我的应用程序。我试图捕捉到一些错误,我意识到我在获取数据并尝试显示它时得到错误。请查看我的代码并尝试帮助我调试它,因为我无法确定我在哪里做错了。

package com.jetas.vpl;

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

`import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;


import org.json.JSONObject;


import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.os.StrictMode;

import android.util.Log;

import android.widget.TextView;

public class Test extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_single_post);
    StrictMode.enableDefaults();
    getData();
}

public void getData() {
    TextView textview = (TextView) findViewById(R.id.newsTitle);
    //TextView textview2 = (TextView) findViewById(R.id.content);
    //textview.setText("Why are you stubborn");
    InputStream isr = null;
    String result = "";
    String url = "http://192.168.1.1/vpl/getnews.php";
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        isr = httpEntity.getContent();
        //textview2.setText("Test Passed");
    } catch (Exception e) {
        Log.e("log_tag", "Error in HTTP Connection" + e.toString());
        textview.setText("Connection Failed");
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                isr, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {

            sb.append(line + "\n");
            //textview2.setText("Test Passed");
        }

        isr.close();
        result = sb.toString();
        //textview2.setText("Test Passed");
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
        textview.setText("Buffer reader problem");
    }

    try {

        //String s = "";
        JSONArray jArray = new JSONArray(result);

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

            String newsTitle = json.getString("newsTitle");
            textview.setText(newsTitle);
            /*s = s + "Title: " + json.getString("newsTitle") + "\n"
                    + "Content: " + json.getString("newsContent") + "\n\n";
            textview.setText(s); */
        }
    } catch (Exception e) {

        Log.e("log_tag", "Error Parsing Data" + e.toString());
        textview.setText("Error in Parsing Data !!!!!");
    }

}
}

1 个答案:

答案 0 :(得分:0)

如果您是从模拟器访问的localhost,请尝试"http://10.0.2.2/vpl/getnews.php"

此外,在单独的线程内执行网络操作,最好使用AsyncTask

这是一个很好的toutorial