【问题标题】:I fail to retrieve data from mysql in my android application我无法在我的 android 应用程序中从 mysql 检索数据
【发布时间】:2014-03-17 09:33:23
【问题描述】:

我是 android 应用程序开发的新手。我正在开发一个将从 mysql 数据库中检索新闻的应用程序。我首先编写了从数据库中提取新闻标题和内容并成功输出 JSON 编码数据的 PHP 脚本,然后在我的应用程序中编写了以下代码,这些代码将获取 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 !!!!!");
    }

}
}

【问题讨论】:

  • 您遇到什么错误?它出现在哪一行?

标签: android mysql json


【解决方案1】:

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

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

这是一个很好的toutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多