问题
In Android, I want to implement a login system and I'm having an error in converting a String
into a JsonObject
. This is my Activity:
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit;
private ProgressDialog pDialog;
static {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://sorifgroupcom.ipage.com/Android/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
user = (EditText) findViewById(R.id.emailLogin);
pass = (EditText) findViewById(R.id.MPLogin);
mSubmit = (Button) findViewById(R.id.BtnLogin);
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.BtnLogin:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", username));
params.add(new BasicNameValuePair("motdepasse", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
Log.i("tesssssssssssssssssssst", "test");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("email", username);
edit.commit();
finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("tesssssssssssssssssssst", "test");
return null;
}
protected void onPostExecute(String file_url) {
pDialog.cancel();
if (file_url != null) {
Toast.makeText(Login.this, "", Toast.LENGTH_LONG).show();
}
}
}
And the LogCat prints the following errors when I put values into TextEdit
:
E/JSON Parser(619): Error parsing data org.json.JSONException:
Value Fatal of type java.lang.String cannot be converted to JSONObject
E/AndroidRuntime(619): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(619): java.lang.RuntimeException: An error occured
while executing doInBackground()
E/AndroidRuntime(619): at android.os.AsyncTask$3.done(AsyncTask.java:299)
E/AndroidRuntime(619): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
E/AndroidRuntime(619): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
E/AndroidRuntime(619): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
E/AndroidRuntime(619): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
E/AndroidRuntime(619): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
E/AndroidRuntime(619): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime(619): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime(619): at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime(619): Caused by: java.lang.NullPointerException
E/AndroidRuntime(619): at org.Soufiane.voyagesociale.Login$AttemptLogin.doInBackground(Login.java:110)
E/AndroidRuntime(619): at org.Soufiane.voyagesociale.Login$AttemptLogin.doInBackground(Login.java:1)
E/AndroidRuntime(619): at android.os.AsyncTask$2.call(AsyncTask.java:287)
E/AndroidRuntime(619): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
E/AndroidRuntime(619): ... 5 more
E/InputDispatcher(163): channel '41868f48 org.Soufiane.voyagesociale/org.Soufiane.voyagesociale.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
E/InputDispatcher(163): Received spurious receive callback for unknown input channel. fd=193, events=0x9
E/BufferQueue(35): [] drainQueueLocked: BufferQueue has been abandoned!
What does this error mean? And how can I fix this?
回答1:
Your error is inside the jsonParser.makeHttpRequest
function.
Probably it is downloading a text from your server, that is not correctly formated.
You have to check what your "http://sorifgroupcom.ipage.com/Android/login.php" is returning.
Im making some request to that URL and i dont get any result (the response has an empty body)
Probably you have to check your php file to see what its returning. If you want, you can try a POST request with this online tool and post here the response body, or you can give me a valid username/password to set as parameters, to see what happens.
also, if you want, you can post the contents of the php file since i would bet money the error is there. Probably your php file is not printing the json
回答2:
The answer is in your question :-
java.lang.String cannot be converted to JSONObject
Obviously you are trying to convert a string into json which is not possible by the way. Look at your code again or debug it and tell the error on which the exception is coming. Not just the line number, the code on that line too.
Secondly, you are using ASYNCTASK so don't write these lines :-
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
It is bad coding to use StrictMode
来源:https://stackoverflow.com/questions/21000069/error-value-fatal-of-type-java-lang-string-cannot-be-converted-to-jsonobject