The setText() is not displaying any data into my text fields.
I also tried displaying just text like this : nameView.setText("temp");
but this didn't work either.
So, I am pasting my code below that fetches data from a PHP script and receives a JSON response. Also, I did check, I am getting the JSON response.
Plus, I have already added the INTERNET permission in the manifest.xml file
java file :
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 file :
<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>
What do I do ?