gpt4 book ai didi

java - android studio json数据解析错误

转载 作者:行者123 更新时间:2023-12-01 10:19:31 25 4
gpt4 key购买 nike

enter image description here给出错误 In EXCEPTION .... json data parsing error in catch in postexecute method 请帮助如何解决。当数据输入其中时,它开始顺利工作,但是当发送数据时,它显示 json 数据解析错误

/**
* Created by Mian on 2/28/2016.
*/
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class SignupActivity extends AsyncTask<String, Void, String> {
private Context context;
public SignupActivity(Context context) {
this.context = context;
}

protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
String fullName = arg0[0];
String userName = arg0[1];
String passWord = arg0[2];
String phoneNumber = arg0[3];
String emailAddress = arg0[4];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?fullname=" + URLEncoder.encode(fullName, "UTF-8");
data += "&username=" + URLEncoder.encode(userName, "UTF-8");
data += "&password=" + URLEncoder.encode(passWord, "UTF-8");
data += "&phonenumber=" + URLEncoder.encode(phoneNumber, "UTF-8");
data += "&emailaddress=" + URLEncoder.encode(emailAddress, "UTF-8");
link = "http://livethuglife.com/signup.php" + data;
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
result = bufferedReader.readLine();
return result;
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
String jsonStr = result.toString();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String query_result = jsonObj.getString("query_result");
if (query_result.equals("SUCCESS")) {
Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show();
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
}
}

最佳答案

最好根据您已发布到服务器表单使用

  1. 一个简单的Java类,带有多个字段,需要将完整的表单数据传送到服务器。就像我定义为 SignUp 类一样。

  2. 然后创建该Class SignUp对象,将对象填充为您所对应的想要发布到服务器

  3. 现在使用 Google gson.jarobject 更改为 JSON(您可以通过在互联网上搜索来下载)

    A Simple Example I have shown to you..

A Simple POJO Class SignUP

Class SignUP{
private String fullname;
private String username;
private String passoword; // Define Number of Variable as per your Need
private String phonenumber;
private String emailaddress;

//Getter and Setters Method.
}

现在实例化您的类并尝试 Google GSON 库 jar 将其解析为 JSON 对象

A Simple Logic to Convert Object of Class SignUP to JSON .

protected String doInBackground(String... arg0) {
String fullName = arg0[0];
String userName = arg0[1];
String passWord = arg0[2];
String phoneNumber = arg0[3];
String emailAddress = arg0[4];

SignUP obj = new SignUP();
obj.setFullName(URLEncoder.encode(fullName, "UTF-8"));
obj.setUserName(URLEncoder.encode(userName, "UTF-8"));
obj.setPassword(URLEncoder.encode(passWord, "UTF-8"););
obj.setPhoneNumber(URLEncoder.encode(phoneNumber,"UTF-8"););
obj.setEmailAddress(URLEncoder.encode(emailAddress, "UTF-8"););

Gson gson = new Gson();

System.out.println(gson.toJson(obj));
}

输出:

{
"fullName":"FullName",
"username":"Lokesh",
"password":"Gupta",
"phonenumber":"73479273423",
"emailaddress":"abc @abc.com"
}

您也可以尝试How to Convert Java Object to JSON Using Google GSON API

关于java - android studio json数据解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699098/

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