gpt4 book ai didi

java - 错误: Value Fatal of type java. lang.String无法转换为JSONObject

转载 作者:行者123 更新时间:2023-12-01 13:38:57 25 4
gpt4 key购买 nike

在 Android 中,我想实现一个登录系统,但在将 String 转换为 JsonObject 时遇到错误。这是我的 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();
}

}

}

当我将值放入 TextEdit 时,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!

这个错误是什么意思?我该如何解决这个问题?

最佳答案

您的错误位于 jsonParser.makeHttpRequest 内功能。它可能正在从您的服务器下载格式不正确的文本。

您必须检查您的“http://sorifgroupcom.ipage.com/Android/login.php ”返回的内容。

我向该 URL 发出一些请求,但没有得到任何结果(响应正文为空)

可能您必须检查您的 php 文件以查看其返回的内容。如果需要,您可以尝试使用 this online tool 发起 POST 请求。并在此处发布响应正文,或者您可以给我一个有效的用户名/密码来设置为参数,看看会发生什么。

此外,如果您愿意,您可以发布 php 文件的内容,因为我敢打赌,错误就在那里。可能你的 php 文件没有打印 json

关于java - 错误: Value Fatal of type java. lang.String无法转换为JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com