gpt4 book ai didi

java - 解析 org.json.jsonException 值
转载 作者:行者123 更新时间:2023-12-01 12:34:51 35 4
gpt4 key购买 nike

我已经尝试了一切,请任何人帮我解决这个代码。它给出了解析 org.json.jsonException 值时出现错误 br type java.lang.String无法转换为 JSONOobject 错误。除了这个错误 logcat 没有显示任何内容。我认为问题出在 Post Login() 方法内,但我无法弄清楚问题是什么。当我点击发送按钮时,问题就出现了。问题可能出在发送按钮和 PostLogin 方法中。我是堆栈溢出的新手,所以如果出现问题,请合作。我花了 3 天的时间来做这件事,但看不出真正的问题是什么。请任何人如何解决它。是紧急状况。

public class Credit extends Activity implements OnClickListener{

int mYear, mMonth, mDay;
EditText Epickupdate;
Reserve r;
String msg,status;
EditText etname_on_card,etcard_number,etcvv,user_id;
String sflno,stime,sdate,sadd,sdrop_add,sinst;
Button send;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.credit);
getActionBar().hide();

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Epickupdate = (EditText)findViewById(R.id.expiry_date);
Epickupdate.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
datepicker();
}
});

user_id=(EditText)findViewById(R.id.user_id);
etname_on_card=(EditText)findViewById(R.id.name_on_card);
etcard_number=(EditText)findViewById(R.id.card_number);

etcvv=(EditText)findViewById(R.id.cvv);

send=(Button)findViewById(R.id.proceedcheck);
send.setOnClickListener(this);
}

public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://atlcity.qa.apps.jasperit.net/index.php");

SharedPreferences pr= getSharedPreferences("reserve", 0);
String sflno=pr.getString("flightno", "null");
String stime=pr.getString("stime", "null");
String sdate=pr.getString("sdate", "null");
String sadd=pr.getString("sadd", "null");
String sdrop_add=pr.getString("sdrop_add", "null");
String sinst=pr.getString("sinst", "null");

try {
// Add user name and password

String sname_on_card = etname_on_card.getText().toString();
String scard_number = etcard_number.getText().toString();
String expiry_dates = Epickupdate.getText().toString();
String cvvs = etcvv.getText().toString();
String suser_id = user_id.getText().toString();

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "makeReservation"));
nameValuePairs.add(new BasicNameValuePair("app_secret", "jkhljkUILJGJkljhkjUGLG87796587687HGKJhghkjKUYGKJHjhgjUYGKUY7865876hgKUYGK"));
nameValuePairs.add(new BasicNameValuePair("flight_number", sflno));
nameValuePairs.add(new BasicNameValuePair("pick_up_time", stime));
nameValuePairs.add(new BasicNameValuePair("pick_up_date",sdate));
nameValuePairs.add(new BasicNameValuePair("pick_up_address",sadd));
nameValuePairs.add(new BasicNameValuePair("drop_off_address", sdrop_add));
nameValuePairs.add(new BasicNameValuePair("special_instruction", sinst));
nameValuePairs.add(new BasicNameValuePair("user_id", suser_id));
nameValuePairs.add(new BasicNameValuePair("name_on_card",sname_on_card));
nameValuePairs.add(new BasicNameValuePair("card_number",scard_number));
nameValuePairs.add(new BasicNameValuePair("expiry_date", expiry_dates));
nameValuePairs.add(new BasicNameValuePair("cvv", cvvs));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("Response", str);
try {
JSONObject reader = new JSONObject(str);
msg = reader.getString("msg");// server le request gareko
status = reader.getString("status");
Toast.makeText(getApplicationContext()," "+msg,Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "parsing error "+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}

@Override
public void onClick(View v) {

String sname_on_card = etname_on_card.getText().toString();
String scard_number = etcard_number.getText().toString();
String expiry_dates = Epickupdate.getText().toString();
String cvvs = etcvv.getText().toString();
String suser_id = user_id.getText().toString();

try {
if (v.getId() == R.id.proceedcheck) {
if (sname_on_card.matches("")) {
Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
return;
}else if(scard_number.matches("")){
Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
return;
}else if(expiry_dates.matches("")){
Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
return;
}else if(cvvs.matches("")){
Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
return;
}else if(suser_id.matches("")){
Toast.makeText(this, "please fill up all the field", Toast.LENGTH_SHORT).show();
return;
}else{
try {
postLoginData();
} catch (Exception e) {
Toast.makeText(getBaseContext(), "postlogin error " +e,Toast.LENGTH_LONG);
e.printStackTrace();
}

}}

} catch (Exception e) {
Toast.makeText(this,"error in onclick"+e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}

}

}

日志猫:

09-05 00:48:11.147: W/Response(1635): <br /><b>Fatal error</b>:  Call to a member function fetch_object() on a non-object in <b>/home/appsjasp/public_html/atlcity.qa/index.php</b> on line <b>124</b><br />
09-05 00:48:11.177: W/System.err(1635): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
09-05 00:48:11.187: W/System.err(1635): at org.json.JSON.typeMismatch(JSON.java:111)
09-05 00:48:11.187: W/System.err(1635): at org.json.JSONObject.<init>(JSONObject.java:159)
09-05 00:48:11.197: W/System.err(1635): at org.json.JSONObject.<init>(JSONObject.java:172)
09-05 00:48:11.197: W/System.err(1635): at example.atlcitylimo.Credit.postLoginData(Credit.java:172)
09-05 00:48:11.197: W/System.err(1635): at example.atlcitylimo.Credit.onClick(Credit.java:247)
09-05 00:48:11.197: W/System.err(1635): at android.view.View.performClick(View.java:4438)
09-05 00:48:11.207: W/System.err(1635): at android.view.View$PerformClick.run(View.java:18422)
09-05 00:48:11.207: W/System.err(1635): at android.os.Handler.handleCallback(Handler.java:733)
09-05 00:48:11.207: W/System.err(1635): at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 00:48:11.207: W/System.err(1635): at android.os.Looper.loop(Looper.java:136)
09-05 00:48:11.207: W/System.err(1635): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-05 00:48:11.207: W/System.err(1635): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 00:48:11.207: W/System.err(1635): at java.lang.reflect.Method.invoke(Method.java:515)
09-05 00:48:11.217: W/System.err(1635): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-05 00:48:11.227: W/System.err(1635): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-05 00:48:11.227: W/System.err(1635): at dalvik.system.NativeStart.main(Native Method)

最佳答案

由于来自服务器的响应不是 JSONObject ,因此引发异常 org.json.JSONException

回应

<br /><b>Fatal error</b>:  Call to a member function fetch_object() on a non-object in <b>/home/appsjasp/public_html/atlcity.qa/index.php</b> on line <b>124</b><br />

响应采用 XML 而不是 JSON,因此在创建 JSONObject 对象并传递 XML 字符串时会触发 org.json.JSONException

关于java - 解析 org.json.jsonException 值 <br 类型 java.lang.String 无法转换为 JSONOobject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678724/

35 4 0

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