错误:值致命java.lang.String类型的,不能被转换为JSONObject的 [英] Error: Value Fatal of type java.lang.String cannot be converted to JSONObject
问题描述
在Android的,我想实现一个登录系统,我在一个转换字符串
到的JSONObject $ C有错误$ C>。这是我的活动:
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();
}
}
}
而LogCat中打印,当我把值放入文本编辑
以下错误:
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?
推荐答案
您的错误是 jsonParser.makeHtt prequest
函数内。
也许它正在下载从你的服务器一文中,未正确格式化。
Your error is inside the jsonParser.makeHttpRequest
function.
Probably it is downloading a text from your server, that is not correctly formated.
您必须检查你的 http://sorifgroupcom.ipage.com/Android/login .PHP 正在恢复。
You have to check what your "http://sorifgroupcom.ipage.com/Android/login.php" is returning.
林做一些请求到URL,我不得到任何结果(响应有一个空的机构)
Im making some request to that URL and i dont get any result (the response has an empty body)
也许你要检查你的PHP文件,查看其返回的东西。如果你愿意,你可以尝试使用这个在线工具并张贴在这里的响应体,或者你可以给我一个有效的用户名/密码设置为参数,看看会发生什么。
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.
另外,如果你愿意,你可以张贴的PHP文件,因为我敢打赌钱内容的错误是存在的。也许你的PHP文件没有打印的json
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
这篇关于错误:值致命java.lang.String类型的,不能被转换为JSONObject的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!