Java 分析数据org.json.JSONException时出错:输入在-LoginActivity的字符0处结束
我正在开发一个需要用户登录的android应用程序,当我运行LoginActivity时,它会给我一个json解析器错误,我认为我的php代码很好,但我需要另一双眼睛来帮助我找到代码中的错误,谢谢 登录活动Java 分析数据org.json.JSONException时出错:输入在-LoginActivity的字符0处结束,java,android,json,android-asynctask,Java,Android,Json,Android Asynctask,我正在开发一个需要用户登录的android应用程序,当我运行LoginActivity时,它会给我一个json解析器错误,我认为我的php代码很好,但我需要另一双眼睛来帮助我找到代码中的错误,谢谢 登录活动 public class LoginActivity extends Activity { Button btnLogin; EditText inputMobile; EditText inputCountry; private static String KEY_SUCCESS =
public class LoginActivity extends Activity {
Button btnLogin;
EditText inputMobile;
EditText inputCountry;
private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_MOBILE = "mobile";
private static String KEY_COUNTRY = "country";
private static String KEY_CREATED_AT = "created_at";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
inputMobile = (EditText) findViewById(R.id.phone);
inputCountry = (EditText) findViewById(R.id.country);
btnLogin = (Button) findViewById(R.id.login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ((!inputMobile.getText().toString().equals(""))
&& (!inputCountry.getText().toString().equals(""))) {
NetAsync(view);
} else if ((!inputMobile.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(),
"Mobile field empty", Toast.LENGTH_SHORT).show();
} else if ((!!inputCountry.getText().toString().equals(""))) {
Toast.makeText(getApplicationContext(),
"Country field empty", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Mobile and Country field are empty",
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(LoginActivity.this);
nDialog.setTitle("Checking Network");
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) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th) {
if (th == true) {
nDialog.dismiss();
new ProcessLogin().execute();
} else {
nDialog.dismiss();
Toast.makeText(getApplicationContext(),
"Error in Network Connection",
Toast.LENGTH_SHORT).show();
}
}
}
/**
* 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 mobile, country;
@Override
protected void onPreExecute() {
super.onPreExecute();
inputMobile = (EditText) findViewById(R.id.phone);
inputCountry = (EditText) findViewById(R.id.country);
mobile = inputMobile.getText().toString();
country = inputCountry.getText().toString();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Logging in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(mobile, country);
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_NAME),
json_user.getString(KEY_MOBILE),
json_user.getString(KEY_COUNTRY),
json_user.getString(KEY_UID),
json_user.getString(KEY_CREATED_AT));
/**
* If JSON array details are stored in SQlite it
* launches the User Panel.
**/
Intent upanel = new Intent(getApplicationContext(),
MainActivity.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pDialog.dismiss();
startActivity(upanel);
/**
* Close Login Screen
**/
finish();
} else {
pDialog.dismiss();
Toast.makeText(getApplicationContext(),
"Incorrect mobile/country",
Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void NetAsync(View view) {
new NetCheck().execute();
}
JSONPaser
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;
}
您可以发布LoginActivity代码吗?请尝试阅读这一行的输出
Log.e(“JSON”,JSON)
在您的jsonparser类中,它是json格式吗?@hareshchelana您现在可以查看登录活动了
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;
}
11-11 06:13:15.086: E/JSON Parser(28899): Error parsing data org.json.JSONException: End of input at character 0 of
11-11 06:13:15.096: D/AndroidRuntime(28899): Shutting down VM
11-11 06:13:15.096: W/dalvikvm(28899): threadid=1: thread exiting with uncaught exception (group=0x418a0da0)
11-11 06:13:15.106: E/AndroidRuntime(28899): FATAL EXCEPTION: main
11-11 06:13:15.106: E/AndroidRuntime(28899): Process: com.calendarapp, PID: 28899
11-11 06:13:15.106: E/AndroidRuntime(28899): java.lang.NullPointerException
11-11 06:13:15.106: E/AndroidRuntime(28899): at com.calendarapp.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:172)
11-11 06:13:15.106: E/AndroidRuntime(28899): at com.calendarapp.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:1)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.os.Looper.loop(Looper.java:157)
11-11 06:13:15.106: E/AndroidRuntime(28899): at android.app.ActivityThread.main(ActivityThread.java:5356)
11-11 06:13:15.106: E/AndroidRuntime(28899): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 06:13:15.106: E/AndroidRuntime(28899): at java.lang.reflect.Method.invoke(Method.java:515)
11-11 06:13:15.106: E/AndroidRuntime(28899): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-11 06:13:15.106: E/AndroidRuntime(28899): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-11 06:13:15.106: E/AndroidRuntime(28899): at dalvik.system.NativeStart.main(Native Method)