setText()未在我的文本字段中显示任何数据。
我也试着像这样显示文字:nameView.setText("temp");
但这也不起作用。
所以,我粘贴下面的代码,从PHP脚本中获取数据并接收JSON响应。另外,我确实检查过,我收到了JSON响应。
另外,我已经在 manifest.xml 文件中添加了 INTERNET 权限
java文件:
package com.example.shreyastripathy.fetchtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
TextView nameView;
TextView ageView;
TextView jobView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults(); //STRICT MODE ENABLED
nameView = (TextView) findViewById(R.id.nametxt);
ageView = (TextView) findViewById(R.id.agetxt);
jobView = (TextView) findViewById(R.id.jobtxt);
getData();
}
public void getData(){
String result = "";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.5.240/work/test.php"); // PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
nameView.setText("Couldn't connect to database");
}
//convert response to string
try{
assert isr != null;
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
//parse json data
try {
String n = "";
String a="";
String j="";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
n = n + "Name : "+json.getString("FirstName")+" "+json.getString("LastName")+"\n";
a= a + "Age : "+json.getInt("Age")+"\n";
j= j + "Job : "+json.getString("Job")+"\n";
}
nameView.setText(n);
ageView.setText(a);
jobView.setText(j);
}
catch (Exception e) {
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}
xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/background">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/container">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nametxt"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/agetxt"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jobtxt"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我该怎么办?
答案 0 :(得分:0)
感谢大家的帮助。我终于弄清楚代码中的问题是什么。 我从这里输入的数据库中提取的列名是错误的。仅仅是一个错字。
答案 1 :(得分:-1)
我认为你应该在线程中运行getData()。
new Thread(){
public void run() {
getData();
};
}.start();`
您还可以检查textColor。也许文字颜色是相同的背景颜色。