Reputation: 159
i am geting this error java.lang.NullPointerException in line 97
i think is for, de json call, because is not getting the data,
how i can create a thread, for get this data first then do the next step.
iam really new in this so any help well be apreciate.
package com.zoada;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// esconde el titlebar de la aplicaci�n
// tiene que estar antes de colocar el mainLayout
requestWindowFeature(Window.FEATURE_NO_TITLE);
// esconde el statusbar de Android
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
JSONArray posiciones = null;
// *** Comienza la comunicaci�n con el servidor
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://zoada.com/blah.php");
HttpResponse response = client.execute(request);
// leyendo "response" un string gigantesco
in = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()));
// imprimpiendo el tarugo de texto linea a linea
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
//System.out.println(page);
//aqui viene la parte JSON!!!
JSONObject jObject = new JSONObject(page);
posiciones = jObject.getJSONArray("positions");
for (int i=0; i< posiciones.length(); i++) {
JSONObject jo = posiciones.getJSONObject(i);
//System.out.println(jo.getString("name"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// *** termina comunicaci�n con el servidor
TableLayout tl = (TableLayout)findViewById(R.id.containerTable);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < posiciones.length(); i++) {
try {
JSONObject jo = posiciones.getJSONObject(i);
TableRow row = (TableRow)inflater.inflate(R.layout.tablerow, tl, false);
TextView pos = (TextView)row.findViewById(R.id.position);
String posTxt=jo.getString("position");
pos.setText(posTxt);
TextView team = (TextView)row.findViewById(R.id.team);
String teamTxt=jo.getString("name");
team.setText(teamTxt);
TextView points = (TextView)row.findViewById(R.id.points);
String pointsTxt=jo.getString("points");
points.setText(pointsTxt);
TextView games = (TextView)row.findViewById(R.id.games);
String gamesTxt=jo.getString("played");
games.setText(gamesTxt);
TextView victories = (TextView)row.findViewById(R.id.victories);
String victoriesTxt=jo.getString("won");
victories.setText(victoriesTxt);
TextView draw = (TextView)row.findViewById(R.id.draw);
String drawsTxt=jo.getString("draw");
draw.setText(drawsTxt);
TextView lost = (TextView)row.findViewById(R.id.loss);
String loseTxt=jo.getString("lost");
lost.setText(loseTxt);
TextView goals = (TextView)row.findViewById(R.id.goals);
int goalsInt = Integer.parseInt(jo.getString("gol"));
int againstInt = Integer.parseInt(jo.getString("against"));
String goalsTxt=goalsInt+":"+againstInt;
goals.setText(goalsTxt);
TextView diff = (TextView)row.findViewById(R.id.difference);
int diffInt=goalsInt-againstInt;
String diffTxt=""+diffInt;
diff.setText(diffTxt);
/**/
tl.addView(row);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// text = (EditText) findViewById(R.id.EditText01);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Upvotes: 1
Views: 1669
Reputation: 819
You shouldn't perform network operations in UI thread! For instance you can create AsyncTask in your class. It may looks similar to this
private class ParseTask extends AsyncTask<Params, Progress, Result> {
@Override
protected Result doInBackground(Params params) {
// get url
Params url = params[0];
// create HttpClient etc.
...
// get response, and parse json
// return
return result;
}
@Override
protected void onPostExecute (Result result) {
// now when you have result from parsed json,
// update application UI or whatever you need
someEditText.setText(value_from_result);
}
}
After that, simply call in onCreate method
ParseTask task = new ParseTask();
task.execute(url);
to start AsyncTask.
On the other hand, it's possible to get the same effect by processing json in IntentService, or service in general, and parsed json return to Activity through broadcast.
Upvotes: 1