使用JSON解析数据并在Android中将其显示为文本

使用JSON解析数据并在Android中将其显示为文本,android,mysql,json,Android,Mysql,Json,我使用JSON访问mySQL数据库,然后返回产品信息,如UPCA、产品和公司。我能够获得以灰色文本显示的信息,但不会显示在文本视图中。如何使其显示在文本视图中。我已经复习了其他问题,但我找不到一个对我特别有帮助的问题。关于如何做到这一点的任何信息都将非常有用。也许我错过了什么?先谢谢你 这是我的主文件包 net.example.glutefree; import android.os.Bundle; import android.app.Activity; import android.con

我使用JSON访问mySQL数据库,然后返回产品信息,如UPCA、产品和公司。我能够获得以灰色文本显示的信息,但不会显示在文本视图中。如何使其显示在文本视图中。我已经复习了其他问题,但我找不到一个对我特别有帮助的问题。关于如何做到这一点的任何信息都将非常有用。也许我错过了什么?先谢谢你

这是我的主文件包

net.example.glutefree;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

//Links GLUTEFREE with NEWACTIVITY
public class GluteFree extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.glute_free);
        mMyEditText = (EditText) findViewById(R.id.editText1);
        Button pressToScan = (Button) findViewById(R.id.button1);

        pressToScan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent data = new Intent("com.google.zxing.client.android.SCAN");
                data.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE",
                        "QR_CODE_MODE");
                setResult(RESULT_OK, data);
                startActivityForResult(data, 0);
            }
        });
    }



    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         String UPC = null;
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                UPC = data.getStringExtra("SCAN_RESULT");
                // moved here
                Intent i = new Intent("net.example.glutefree.Networking");
                i.putExtra("UPCA", UPC);
                startActivity(i);
                EditText tv = (EditText) findViewById(R.id.editText1);
                tv.setText(UPC);
                // Handle successful scan
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }

        }

    }

    // Click this Link to open Links Page
    public void onClickLinks(View view) {
        ;
    }
    //Click this Link to open the Previous Result
    public void onClickResult(View view) {
        startActivity(new Intent("net.example.glutefree.Networking"));
    }
    //Manually Search the UPC that is in the box
    //FIND CODE TO MAKE THAT WORK
    private EditText mMyEditText;
    public void onClickSearch(View view) { 

        String UPC = mMyEditText.getText().toString();
        Intent intent = new Intent(this, Networking.class);   
        intent.putExtra("UPCA", UPC);  //text is some key used to retrieve value in NextActivity 
        startActivity(intent);
    }
}
这是我的数据库连接文件

package net.example.glutefree;

import android.app.Activity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.TextView;





public class Networking extends Activity{
    TextView txt;
    int request_Code = 1;
//called when activity is first created
    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_networking);
    txt = new TextView(getApplicationContext());
    // Set the text and call the connect function. 
    txt.setText("Connecting...");
  //call the method to run the data retrieval
    new Thread() {
        public void run() {
            final String data = getServerData(KEY_121);
            if (data != null)
                runOnUiThread(new Runnable()
                {
                    public void run()
                    {
                       txt.setText(data);

                    }
                });
        }
    }.start();
}



public static final String KEY_121 = "http://website.com/scripts/application_query.php";

private String getServerData(String returnString) {
   String UPC = getIntent().getStringExtra("UPCA");
   InputStream is = null;
   String result = "";
    //the upc data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("UPCA",UPC));

    //http post
    try{

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

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

    //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),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());
    }
    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);
            Log.e("log_tag", "Result ");
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    String UPCA = json_data.getString("UPCA");
                    String Product = json_data.getString("Product");
                    String Glute = json_data.getString("Gluten Free");
                    returnString += "\n\t" + jArray.getJSONObject(i);
            }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString;
}

}
package net.example.free;
导入android.app.Activity;
导入android.os.Bundle;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.util.Log;
导入android.widget.TextView;
公共课堂网络扩展了活动{
文本视图;
int请求_代码=1;
//首次创建活动时调用
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u网络);
txt=新文本视图(getApplicationContext());
//设置文本并调用connect函数。
txt.setText(“连接…”);
//调用该方法以运行数据检索
新线程(){
公开募捐{
最终字符串数据=getServerData(键121);
如果(数据!=null)
runOnUiThread(新的Runnable()
{
公开募捐
{
txt.setText(数据);
}
});
}
}.start();
}
公共静态最终字符串键_121=”http://website.com/scripts/application_query.php";
私有字符串getServerData(字符串返回字符串){
字符串UPC=getIntent().getStringExtra(“UPCA”);
InputStream=null;
字符串结果=”;
//要发送的upc数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“UPCA”,UPC));
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(键号121);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
Log.e(“日志标签”、“结果”);
对于(int i=0;i