我正在使用 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;
}
}