Android : erreur dans l'Utilisation d' AsyncTask
Bonsoir tout le monde
je travail sur une application qui permet de lire un fichier de type Json sur un serveur distant et d'afficher le contenu du fichier ...
j'ai utilisé l'exemple du fichier http://jsonparsing.parseapp.com/json...esDemoItem.txt
Je ne parviens pas a interpréter les erreurs du log et a trouver une solution
aidez moi svp !!!:calim2:
voici mon code MainActivity.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {
private TextView tvdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHit = (Button)findViewById(R.id.btnHit);
TextView tvdata = (TextView)findViewById(R.id.tvJsonItem);
btnHit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JSONTask().execute("http://http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt");
}
//tvdata.setText("gabin");
});
}
public class JSONTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
//URL url = new URL("http://10.0.2.2/andro/get.txt");
URL url = new URL(params[0]);
//URL url = new URL("10.0.2.2/get.txt");
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
String line = "";
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line + "");
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tvdata.setText(result);
}
}
} |
et celui de activity xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="parsing.json.com.jsonparsindemo.MainActivity">
<Button
android:id="@+id/btnHit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:text="Hit"/>
<TextView
android:id="@+id/tvJsonItem"
android:layout_margin="5sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout> |
et les erreurs
09-10 21:25:11.372 6991-6991/parsing.json.com.jsonparsindemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: parsing.json.com.jsonparsindemo, PID: 6991
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at parsing.json.com.jsonparsindemo.MainActivity$JSONTask.onPostExecute(MainActivity.java:87)
at parsing.json.com.jsonparsindemo.MainActivity$JSONTask.onPostExecute(MainActivity.java:39)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
bien évidemment j'ai autorisé la permission INTERNET
Merci de m'aider