java object... 出错,错误:值致命java.lang.String类型的,不能被转换为JSONObject的

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 {

@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 params = new ArrayList();

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?

解决方案

Your error is inside the jsonParser.makeHttpRequest function.

Probably it is downloading a text from your server, that is not correctly formated.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值