Hey Leute ich vesuche Daten von einem PHP Script in meine App anzuzeigen dabei erhalte ich einen Fehler auf den ich keine Lösung finden kann.
Hier der Code der Klasse:
Und hier das Stacktrace:
Hier der Code der Klasse:
Java:
package com.thegamefactory.geochallenge;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.thegamefactory.beans.Score;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class HighscoreActivity extends Activity {
private LinearLayout linearScore, linearName;
private TextView textViewScore, textViewName;
private ScrollView scrollView;
private static String LINK = "****";
private int score;
private TextView textViewPersonalBest;
private boolean isNewBest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_highscore);
linearName = (LinearLayout) findViewById(R.id.linearName);
linearScore = (LinearLayout) findViewById(R.id.linearScore);
scrollView = (ScrollView) findViewById(R.id.scrollView);
textViewPersonalBest = (TextView) findViewById(R.id.textViewPersonalBest);
score = getIntent().getExtras().getInt("score");
isNewBest = false;
if (score > getPersonalBest()) {
setPersonalBest(score);
isNewBest = true;
}
textViewPersonalBest.setText(getResources().getString(R.string.personal_best) + getPersonalBest());
if (isNetworkAvailable())
writeScores();
else
showError(getResources().getString(R.string.no_conn_error));
if (isNewBest)
showError(getResources().getString(R.string.new_personal_best_msg));
}
private int getPersonalBest() {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
int personalBest = preferences.getInt("personalBest", 0);
return personalBest;
}
private void setPersonalBest(int updateScore) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("personalBest", updateScore);
editor.commit();
}
@SuppressWarnings("static-access")
private void showError(String msg) {
new Toast(getApplicationContext()).makeText(getApplicationContext(),
msg, Toast.LENGTH_SHORT).show();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private void writeScores() {
linearScore.removeAllViews();
linearName.removeAllViews();
scrollView.fullScroll(ScrollView.FOCUS_UP);
DataGet dataget = new DataGet();
dataget.execute(LINK);
ArrayList<Score> liste = null;
try {
liste = dataget.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
int place = 1;
for (int i = 0; i < liste.size(); i++) {
String score = liste.get(i).getScore();
String name = liste.get(i).getName();
textViewName = new TextView(getApplicationContext());
textViewName.setText(place + ". " + name);
textViewName.setTextSize(20);
linearName.addView(textViewName);
textViewScore = new TextView(getApplicationContext());
textViewScore.setText(score);
textViewScore.setTextSize(20);
linearScore.addView(textViewScore);
place++;
}
}
}
class DataGet extends AsyncTask<String, Void, ArrayList<Score>> {
@Override
protected ArrayList<Score> doInBackground(String... urls) {
InputStream is = null;
String result = "";
JSONObject json_data;
ArrayList<Score> results = new ArrayList<Score>();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urls[0]);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Fehler bei der http Verbindung " + e.toString());
}
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());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
Score score = new Score((String) json_data.get("nom"),
(String) json_data.get("score"));
results.add(score);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return results;
}
}
Und hier das Stacktrace:
03-23 22:35:32.914: W/CursorWrapperInner(21039): Cursor finalized without prior close()
03-23 22:35:33.304: W/System.err(21039): java.util.concurrent.ExecutionException: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String
03-23 22:35:33.304: W/System.err(21039): at java.util.concurrent.FutureTask.report(FutureTask.java:94)
03-23 22:35:33.304: W/System.err(21039): at java.util.concurrent.FutureTask.get(FutureTask.java:160)
03-23 22:35:33.304: W/System.err(21039): at android.os.AsyncTask.get(AsyncTask.java:482)
03-23 22:35:33.304: W/System.err(21039): at com.thegamefactory.geochallenge.HighscoreActivity.writeScores(HighscoreActivity.java:106)
03-23 22:35:33.304: W/System.err(21039): at com.thegamefactory.geochallenge.HighscoreActivity.onCreate(HighscoreActivity.java:62)
03-23 22:35:33.304: W/System.err(21039): at android.app.Activity.performCreate(Activity.java:5133)
03-23 22:35:33.304: W/System.err(21039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-23 22:35:33.304: W/System.err(21039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
03-23 22:35:33.314: W/System.err(21039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
03-23 22:35:33.314: W/System.err(21039): at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-23 22:35:33.314: W/System.err(21039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
03-23 22:35:33.314: W/System.err(21039): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 22:35:33.314: W/System.err(21039): at android.os.Looper.loop(Looper.java:213)
03-23 22:35:33.314: W/System.err(21039): at android.app.ActivityThread.main(ActivityThread.java:5225)
03-23 22:35:33.314: W/System.err(21039): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 22:35:33.314: W/System.err(21039): at java.lang.reflect.Method.invoke(Method.java:525)
03-23 22:35:33.314: W/System.err(21039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-23 22:35:33.314: W/System.err(21039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-23 22:35:33.314: W/System.err(21039): at dalvik.system.NativeStart.main(Native Method)
03-23 22:35:33.314: W/System.err(21039): Caused by: java.lang.ClassCastException: org.json.JSONObject$1 cannot be cast to java.lang.String
03-23 22:35:33.314: W/System.err(21039): at com.thegamefactory.geochallenge.DataGet.doInBackground(HighscoreActivity.java:172)
03-23 22:35:33.314: W/System.err(21039): at com.thegamefactory.geochallenge.DataGet.doInBackground(HighscoreActivity.java:1)
03-23 22:35:33.314: W/System.err(21039): at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-23 22:35:33.314: W/System.err(21039): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-23 22:35:33.314: W/System.err(21039): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-23 22:35:33.324: W/System.err(21039): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-23 22:35:33.324: W/System.err(21039): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-23 22:35:33.324: W/System.err(21039): at java.lang.Thread.run(Thread.java:841)
03-23 22:35:33.324: D/AndroidRuntime(21039): Shutting down VM
03-23 22:35:33.324: W/dalvikvm(21039): threadid=1: thread exiting with uncaught exception (group=0x415de8b0)
03-23 22:35:33.324: E/AndroidRuntime(21039): FATAL EXCEPTION: main
03-23 22:35:33.324: E/AndroidRuntime(21039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thegamefactory.geochallenge/com.thegamefactory.geochallenge.HighscoreActivity}: java.lang.NullPointerException
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.os.Looper.loop(Looper.java:213)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread.main(ActivityThread.java:5225)
03-23 22:35:33.324: E/AndroidRuntime(21039): at java.lang.reflect.Method.invokeNative(Native Method)
03-23 22:35:33.324: E/AndroidRuntime(21039): at java.lang.reflect.Method.invoke(Method.java:525)
03-23 22:35:33.324: E/AndroidRuntime(21039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
03-23 22:35:33.324: E/AndroidRuntime(21039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-23 22:35:33.324: E/AndroidRuntime(21039): at dalvik.system.NativeStart.main(Native Method)
03-23 22:35:33.324: E/AndroidRuntime(21039): Caused by: java.lang.NullPointerException
03-23 22:35:33.324: E/AndroidRuntime(21039): at com.thegamefactory.geochallenge.HighscoreActivity.writeScores(HighscoreActivity.java:113)
03-23 22:35:33.324: E/AndroidRuntime(21039): at com.thegamefactory.geochallenge.HighscoreActivity.onCreate(HighscoreActivity.java:62)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.Activity.performCreate(Activity.java:5133)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-23 22:35:33.324: E/AndroidRuntime(21039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
03-23 22:35:33.324: E/AndroidRuntime(21039): ... 11 more
Zuletzt bearbeitet: