不通过json解析获取数据。我想从url获取数据并将其设置为textview。请帮忙
private static final String URL_PRODUCTS = "http://ebeautyapp.com/experts/getContactUs.php";
//method for json parcing
private void loadData() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener < String > () {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
String result = jObj.getString("result");
if (result.equals("success")) {
JSONArray jsonArray = jObj.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String contact_id = jsonObject.getString("contact_id");
String fullname = jsonObject.getString("fullname");
String moble1 = jsonObject.getString("moble1");
String mobileno2 = jsonObject.getString("mobileno2");
String profile_pic = jsonObject.getString("profile_pic");
String address = jsonObject.getString("address");
String about_us = jsonObject.getString("about_us");
fulllnames.setText(fullname);
mobilenos.setText(moble1);
}
} else {
String status = jObj.getString("status");
Toast.makeText(getApplicationContext(), "" + status, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
答案 0 :(得分:0)
使用这个我得到了服务器的响应。希望这能解决你的问题。
class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
protected void onPreExecute() {
// responseView.setText("");
}
protected String doInBackground(Void... urls) {
String API_URL = "http://ebeautyapp.com/experts/getContactUs.php";
// Do some validation here
try {
URL url = new URL(API_URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
// progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
// responseView.setText(response);
// parseJsonData(response);
}
JSON解析
private void jsonParsing(String response){
try {
JSONObject jObj = new JSONObject(response);
String result = jObj.getString("result");
if (result.equals("success")) {
JSONArray jsonArray = jObj.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String contact_id = jsonObject.getString("contact_id");
String fullname = jsonObject.getString("fullname");
String moble1 = jsonObject.getString("moble1");
String mobileno2 = jsonObject.getString("mobileno2");
String profile_pic = jsonObject.getString("profile_pic");
String address = jsonObject.getString("address");
String about_us = jsonObject.getString("about_us");
fulllnames.setText(fullname);
mobilenos.setText(moble1);
}
} else {
String status = jObj.getString("status");
Toast.makeText(getApplicationContext(), "" + status, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
使用此
使用此任务非常简单 new RetrieveFeedTask().execute();