解析json時出現錯誤。我一直在一些帖子,但顯然這個問題是由幾個原因造成的。謝謝你給我一點時間。 我的目標是內容類型發送到EDITTEXT到decais返回類型信息的內容進入了一個php文件是不正確或不Android Json錯誤解析數據
這是我的登錄類
package com.example.androidapp3;
import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.androidapp3.library.DatabaseHandler;
import com.example.androidapp3.library.UserFunctions;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Login extends Activity {
Button btnLogin;
EditText inputCodePin;
private TextView loginErrorMsg;
/**
* Called when the activity is first created.
*/
private static String KEY_SUCCESS = "success";
private static final String KEY_NOM = "nom";
private static final String KEY_EMAIL = "email";
private static final String KEY_CODEPIN = "codepin";
private static final String KEY_ROLE = "role";
private static final String KEY_PHONE = "phone";
private static final String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = (Button) findViewById(R.id.login);
loginErrorMsg = (TextView) findViewById(R.id.loginErrorMsg);
inputCodePin = (EditText) findViewById(R.id.codepin);
/**
* Login button click event
**/
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!TextUtils.isEmpty(inputCodePin.getText().toString())) {
NetAsync(view);
} else {
Toast.makeText(getApplicationContext(), "Veuillez entrer un code pin valide svp", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Async Task to check whether internet connection is working.
**/
private class NetCheck extends AsyncTask<String,String,Boolean>
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(Login.this);
nDialog.setTitle("Verification réseau");
nDialog.setMessage("Loading..");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
@Override
protected Boolean doInBackground(String... args){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
new ProcessLogin().execute();
}
else{
nDialog.dismiss();
loginErrorMsg.setText("Verifiez votre connexion");
}
}
}
/**
* Async Task to get and send data to My Sql database through JSON respone.
**/
private class ProcessLogin extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
String codepin;// = inputCodePin.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
inputCodePin = (EditText) findViewById(R.id.codepin);
pDialog = new ProgressDialog(Login.this);
pDialog.setTitle("Vérification de données");
pDialog.setMessage("Patientez ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(codepin);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
pDialog.setMessage("Loading User Space");
pDialog.setTitle("Getting Data");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
/**
* Clear all previous data in SQlite database.
**/
UserFunctions logout = new UserFunctions();
logout.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NOM),json_user.getString(KEY_EMAIL),json_user.getString(KEY_CODEPIN),json_user.getString(KEY_PHONE),json_user.getString(KEY_ROLE),json_user.getString(KEY_CREATED_AT));
/**
*If JSON array details are stored in SQlite it launches the User Panel.
**/
Intent upanel = new Intent(getApplicationContext(), Main.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pDialog.dismiss();
startActivity(upanel);
/**
* Close Login Screen
**/
finish();
}else{
pDialog.dismiss();
loginErrorMsg.setText("Code Pin Incorrect");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void NetAsync(View view){
new NetCheck().execute();
}
}
// JSONParser
package com.example.androidapp3.library;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
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();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
//登錄
09-30 07:17:34.010: E/JSON Parser(8900): Error parsing data org.json.JSONException: End of input at character 0 of
09-30 07:17:34.010: W/dalvikvm(8900): threadid=1: thread exiting with uncaught exception (group=0x40a799d8)
09-30 07:17:34.010: E/AndroidRuntime(8900): FATAL EXCEPTION: main
09-30 07:17:34.010: E/AndroidRuntime(8900): java.lang.NullPointerException
09-30 07:17:34.010: E/AndroidRuntime(8900): at com.example.androidapp3.Login$ProcessLogin.onPostExecute(Login.java:168)
09-30 07:17:34.010: E/AndroidRuntime(8900): at com.example.androidapp3.Login$ProcessLogin.onPostExecute(Login.java:1)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.os.AsyncTask.finish(AsyncTask.java:602)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.os.AsyncTask.access$600(AsyncTask.java:156)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.os.Looper.loop(Looper.java:137)
09-30 07:17:34.010: E/AndroidRuntime(8900): at android.app.ActivityThread.main(ActivityThread.java:4471)
09-30 07:17:34.010: E/AndroidRuntime(8900): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 07:17:34.010: E/AndroidRuntime(8900): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 07:17:34.010: E/AndroidRuntime(8900): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
09-30 07:17:34.010: E/AndroidRuntime(8900): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
09-30 07:17:34.010: E/AndroidRuntime(8900): at dalvik.system.NativeStart.main(Native Method)
顯示什麼是你的行號:'Login $ ProcessLogin.onPostExecute(Login.java:168)'? – GrIsHu 2014-09-30 08:42:15
檢查響應..可能你收到服務器的空白響應。 – extmkv 2014-09-30 08:42:44
什麼是行Login.java:168? – 2014-09-30 08:42:54