Fehler mit dem Wert <br vom Typ java.lang.String kann nicht in JSONObject konvertiert werden
Ich habe ein Problem mit der Zeichenfolge, die nicht in JSONObject konvertiert werden kann. Kann jemand helfen, dieses Problem zu lösen? Vielen Dank und sehr dankbar für Ihre Hilfe.
<?php
mysql_connect("localhost","test","");
mysql_select_db("test");
$response = array();
$fullname=$_POST["fullname"];
$result = mysql_query("select fullname,contact, DATE_FORMAT(reserveDate,'%e-%c-%Y') reserveDate,
DATE_FORMAT(reserveTime,'%h:%i %p') reserveTime,
pax,tableNumber
from reserve inner join customer using (customerID)
where fullname=$fullname" );
if (mysql_num_rows($result) > 0) {
$response["reserve"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$reserv = array();
$reserv["fullname"] = $row["fullname"];
$reserv["contact"] = $row["contact"];
$reserv["reserveDate"] = $row["reserveDate"];
$reserv["reserveTime"] = $row["reserveTime"];
$reserv["pax"] = $row["pax"];
$reserv["tableNumber"] = $row["tableNumber"];
// push single product into final response array
array_push($response["reserv"], $reserv);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
?>
JSONPARSER-Klasse
package com.example.easy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
//json = sb.toString();
json = sb.toString().substring(0, sb.toString().length()-1);
System.out.println("jsonstring ="+json);
is.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
In der ReserveInfo-Klasse ist ein Fehler aufgetreten. Diesen Fehlerwert abrufen.
package com.example.easy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class Custname extends Activity implements OnClickListener {
TextView custinfo2,lblempty2;
Button btnsearch1;
TextView custinfo,lblempty;
ScrollView svcustdetails, svreserve;
AutoCompleteTextView txtcustname ;
String Contacts[];
int arraysize;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONParser jParser2 = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_RESERV = "reserv";
private static final String TAG_FULLNAME = "fullname";
private static final String TAG_CONTACT = "contact";
private static final String TAG_DATE = "reserveDate";
private static final String TAG_TIME = "reserveTime";
private static final String TAG_PAX = "pax";
private static final String TAG_TABLE = "tableNumber";
JSONArray reserv = null;
JSONArray reserv2 = null;
JSONObject json2;
String contactno;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custname);
btnsearch1= (Button) findViewById(R.id.btnsearch);
svcustdetails=(ScrollView)findViewById(R.id.svcustdetails);
svreserve=(ScrollView)findViewById(R.id.svreserve);
custinfo2=(TextView)findViewById(R.id.custinfo2);
txtcustname = (AutoCompleteTextView)findViewById(R.id.txtcustname);
txtcustname = (AutoCompleteTextView)findViewById(R.id.txtcustname);
btnsearch1= (Button) findViewById(R.id.btnsearch1);
lblempty2= (TextView)findViewById(R.id.lblempty2);
new LoadContact().execute();
btnsearch1.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if(txtcustname.getText().toString().equalsIgnoreCase("")){
Toast.makeText(Custname.this,"Please enter customer name", Toast.LENGTH_SHORT).show();
}else{
reset();
new ReserveInfo().execute();
}
}
});
}
class LoadContact extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest("http://10.0.2.2/Laicamproject/checkName.php", "GET", params);
// Check log cat for JSON reponse
Log.d("All Name: ", json.toString());
Contacts=new String[arraysize];
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
reserv = json.getJSONArray(TAG_RESERV);
arraysize=reserv.length();
Contacts=new String[arraysize];
for(int a=0;a<arraysize;a++){
JSONObject c = reserv.getJSONObject(a);
Contacts[a]=c.getString(TAG_FULLNAME);
}
} else {
System.out.println("no user found");
}
} catch (JSONException e) {
System.out.println("e3"+e.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
//ArrayList with duplicates String
List<String> dupList = (List<String>) Arrays.asList(Contacts);
//Converting ArrayList to HashSet to remove duplicates
LinkedHashSet<String> listToSet = new LinkedHashSet<String>(dupList);
//Creating Arraylist without duplicate values
List<String> contlist = new ArrayList<String>(listToSet);
System.out.println("Customer list: " + contlist );
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Custname.this,android.R.layout.simple_dropdown_item_1line,contlist );
// Set the adapter
txtcustname.setAdapter(adapter);
txtcustname.setThreshold(1);
}
}
class ReserveInfo extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Custname.this);
pDialog.setMessage("Loading reservation");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
String n=txtcustname.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_FULLNAME,n ));
json2 = jParser2.makeHttpRequest("http://10.0.2.2/Laicamproject/cName.php","POST", params);
System.out.println("params"+params);
try {
reserv2 = json2.getJSONArray(TAG_RESERV);
System.out.println("Reservation"+reserv2.toString());
int i=0;
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("e1=="+e.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
int num=1;
JSONObject c2;
try {
int success = json2.getInt(TAG_SUCCESS);
if (success == 1) {
for(int i=0;i<reserv2.length();i++){
c2 = reserv2.getJSONObject(i);
//System.out.println("c2.getString(TAG_CONTACT).==="+c2.getString(TAG_CONTACT));
//System.out.println("c2.getString(TAG_FULLNAME).==="+c2.getString(TAG_FULLNAME));
custinfo2.setText("Name \t: "+ c2.getString(TAG_FULLNAME)
+"\nPhone \t: " + c2.getString(TAG_CONTACT));
lblempty2.append("\n["+num+"] "+"Date \t\t: "+ c2.getString(TAG_DATE)
+"\n\t Time \t\t: " + c2.getString(TAG_TIME)
+"\n\t Pax \t\t\t: "+ c2.getString(TAG_PAX)
+"\n\t Table \t\t: "+ c2.getString(TAG_TABLE)+"\n");
}
}else{
showAlert();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("e2=="+e.toString());
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void showAlert(){
Custname.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Custname.this);
builder.setTitle("No contact number found")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
txtcustname.setText("");
reset();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void reset(){
lblempty2.setText("");
custinfo2.setText("");
}
}