【发布时间】:2017-01-22 13:42:03
【问题描述】:
我正在开发一个应用程序并发出一个运行良好的 http 请求,
这是我的异步类 Doinbackground 方法:
@Override
protected String doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.d("Response ==>", "Response from url: ");
longInfo(jsonStr);
return jsonStr;
}
这是我正在使用的逻辑
public void longInfo(String str) {
if(str.length() > 4000) {
Log.e("TAG", str.substring(0, 4000));
longInfo(str.substring(4000));
} else
Log.e("TAG", str);
}
但这给了我大量的 json 字符串,它破坏了 json 格式,我无法相应地解析对象和数组。请帮忙
这里是我的json解析方法
public void parsejsonp(String jsonp){
if (jsonp != null) {
try {
JSONObject jsonObj = new JSONObject(jsonp);
int count = jsonObj.getInt("count");
Log.e("count=> ",count+"");
int count_total = jsonObj.getInt("count_total");
Log.e("count_total=> ",count_total+"");
int pages = jsonObj.getInt("pages");
Log.e("pages=> ",pages+"");
// Getting JSON Array node
JSONArray Posts = jsonObj.getJSONArray("posts");
// looping through All Posts
for (int i = 0; i < Posts.length(); i++) {
JSONObject c = Posts.getJSONObject(i);
String id = c.getString("id");
String type = c.getString("type");
String slug = c.getString("slug");
String status = c.getString("status");
String title = c.getString("title");
String title_plain = c.getString("title_plain");
String content = c.getString("content");
String date = c.getString("date");
String modified = c.getString("modified");
// Phone node is JSON Object
// JSONObject phone = c.getJSONObject("phone");
// String mobile = phone.getString("mobile");
// String home = phone.getString("home");
// String office = phone.getString("office");
// tmp hash map for single contact
HashMap<String, String> posting = new HashMap<>();
// adding each child node to HashMap key => value
posting.put("id", id);
posting.put("type", type);
posting.put("slug", slug);
posting.put("status",status);
posting.put("title", title);
posting.put("content", content);
posting.put("date", date);
posting.put("modified", modified);
// adding contact to contact list
postList.add(posting);
}
} catch (final JSONException e) {
Log.e("error", "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e("er server", "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
}
这是我的 onPost 方法:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("RESULT",result);
parsejsonp(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), postList,
R.layout.list_item2, new String[]{"type", "slug","status",
"title","content","date","modified"}, new int[]{R.id.type,
R.id.slug,R.id.state_p,R.id.title_p, R.id.content,R.id.date_p,R.id.moddate_p});
lv.setAdapter(adapter);
}
但我仍然无法解析所有数据,因为仍然在我的 onPost 方法中的结果参数中我没有完整的响应
我还检查了浏览器上的 url,响应完全显示在那里。 但是如果由于缓冲区大小而没有完全显示在 logcat 中,它必须将所有数据放在我的列表视图中,但它不是
这是我的列表视图的屏幕截图
您可以看到只插入了一个对象数据但未插入其余响应,我已经调试了所有代码并检查了它存储完整对象数量的 arraylist 大小,但我的屏幕似乎只显示一个对象数据:
这是我的 layout.xml 文件,问题可能就在这里:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment">
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.adnan.zwd.hidoctor.Patient.Frag_two">
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:text="type"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/slug"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="slug"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/state_p"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="status"
android:paddingBottom="2dip"
android:textColor="@color/colorAccent" />
<TextView
android:id="@+id/title_p"
android:text="title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="@+id/content"
android:text="content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="@+id/date_p"
android:text="date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
<TextView
android:id="@+id/moddate_p"
android:text="Mod Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:textStyle="bold" />
</LinearLayout>
关于这个问题的任何帮助,我在互联网上搜索了很多但没有成功
【问题讨论】:
-
为什么要在这里剪断字符串 str.substring(0, 4000) ???
-
我从这里得到了这个解决方案stackoverflow.com/questions/35546209/…
-
我认为 String 可以容纳的最大大小是 4000 这就是为什么实际上我的 json 响应真的很大
-
我认为 String 可以容纳的最大大小是 4000 这就是为什么实际上我的 json 响应真的很大