我是android studio的新手,而且我不知道如何解决此问题。 我正在尝试根据我的数据创建在线列表视图。 我使用okhttp在服务器和应用程序之间建立了连接,但是当我向服务器发送请求时,出现此错误:
类型为java.lang.String的值br不能转换为JSONObject
这是我使用okhttp与服务器的连接:
public class JSONParser {
private static final String KEY_USER_ID = "user_id";
/**
* Response
*/
private static Response response;
/**
* Get Data From WEB
*
* @return JSON Object
*/
public static JSONObject getDataFromWeb() {
try {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"action\":\"appointmentlist\",\"tokenid\":\"j93F03JjfW0kdS\",\"user\":\"apptester\",\"pass\":\"123456\"}");
Request request = new Request.Builder()
.url("***")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("Postman-Token", "ffcce4c8-7904-4a1d-a2dd-88b94ae8185f")
.build();
response = client.newCall(request).execute();
return new JSONObject(response.body().string());
} catch (@NonNull IOException | JSONException e) {
Log.e(TAG, "" + e.getLocalizedMessage());
}
return null;
}
这是我的主要活动
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<MyDataModel> list;
private MyArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Array List for Binding Data from JSON to this List
*/
list = new ArrayList<>();
/**
* Binding that List to Adapter
*/
adapter = new MyArrayAdapter(this, list);
/**
* Getting List and Setting List Adapter
*/
/**
* Just to know onClick and Printing Hello Toast in Center.
*/
Toast toast = Toast.makeText(getApplicationContext(), "Click on FloatingActionButton to Load JSON", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull View view) {
/**
* Checking Internet Connection
*/
if (InternetConnection.checkConnection(getApplicationContext())) {
new GetDataTask().execute();
} else {
Snackbar.make(view, "Internet Connection Not Available", Snackbar.LENGTH_LONG).show();
}
}
});
}
/**
* Creating Get Data Task for Getting Data From Web
*/
class GetDataTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
/**
* Progress Dialog for User Interaction
*/
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("Hey Wait Please...");
dialog.setMessage("I am getting your JSON");
dialog.show();
}
@Nullable
@Override
protected Void doInBackground(Void... params) {
/**
* Getting JSON Object from Web Using okHttp
*/
JSONObject jsonObject = JSONParser.getDataFromWeb();
try {
/**
* Check Whether Its NULL???
*/
if (jsonObject != null) {
/**
* Check Length...
*/
if(jsonObject.length() > 0) {
/**
* Getting Array named "contacts" From MAIN Json Object
*/
JSONArray array = jsonObject.getJSONArray(Keys.KEY_DATA);
/**
* Check Length of Array...
*/
int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {
/**
* Creating Every time New Object
* and
* Adding into List
*/
MyDataModel model = new MyDataModel();
/**
* Getting Inner Object from contacts array...
* and
* From that We will get Name of that Contact
*
*/
JSONObject innerObject = array.getJSONObject(jIndex);
String name = innerObject.getString(Keys.KEY_NAME);
String pack = innerObject.getString(Keys.KEY_PACK);
String unit = innerObject.getString(Keys.KEY_UNIT);
String brorgn = innerObject.getString(Keys.KEY_BR_OR_GN);
String price = innerObject.getString(Keys.KEY_PRICE);
String message = innerObject.getString(Keys.KEY_MSG);
String phone = phoneObject.getString(Keys.KEY_MOBILE);
model.setName(name);
model.setPack(pack);
model.setUnit(unit);
model.setBrorgn(brorgn);
model.setPrice(price);
model.setMessage(message);
/**
* Adding name and phone concatenation in List...
*/
list.add(model);
}
}
}
} else {
}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "bia inm bekhon dige" + je.getLocalizedMessage());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(list.size() > 0) {
adapter.notifyDataSetChanged();
} else {
Snackbar.make(findViewById(R.id.parentLayout), "No Data Found", Snackbar.LENGTH_LONG).show();
}
}
}
和我的日志:
2019-11-30 19:33:53.797 3127-5808/com.twilio.adv E/TAG: Value <br of type java.lang.String cannot be converted to JSONObject
我添加了这些以查找问题
Log.e(TAG, "getLocalizedMessage :" + e.getLocalizedMessage());
Log.e(TAG, "Response : " + response);
Log.e(TAG, "getMessage : " + e.getMessage());
----------,-------