我正在开发一个显示来自mysql数据库的数据的ListView,但是我在logcat中收到以下错误:
如果需要8k-char缓冲区,最好是明确的.
谁能帮我这个?
这是logcat中的行:
07-16 03:49:16.388:I / global(391):BufferedReader构造函数中使用的默认缓冲区大小.如果需要8k-char缓冲区,最好是明确的.
我的Java代码
package com.database_demo;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class Database_demo extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
List<String> r = new ArrayList<String>();
try{
//http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.2");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//Convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
}
//END Convert response to string
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
r.add(json_data.getString("category"));
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
}
catch(JSONException e1){
Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
}
}
}
nullpointer exeption
07-16 04:02:28.708: D/AndroidRuntime(417): Shutting down VM
07-16 04:02:28.708: W/dalvikvm(417): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-16 04:02:28.728: E/AndroidRuntime(417): FATAL EXCEPTION: main
07-16 04:02:28.728: E/AndroidRuntime(417): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.database_demo/com.database_demo.Database_demo}: java.lang.NullPointerException
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.os.Looper.loop(Looper.java:123)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-16 04:02:28.728: E/AndroidRuntime(417): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 04:02:28.728: E/AndroidRuntime(417): at java.lang.reflect.Method.invoke(Method.java:521)
07-16 04:02:28.728: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-16 04:02:28.728: E/AndroidRuntime(417): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-16 04:02:28.728: E/AndroidRuntime(417): at dalvik.system.NativeStart.main(Native Method)
07-16 04:02:28.728: E/AndroidRuntime(417): Caused by: java.lang.NullPointerException
07-16 04:02:28.728: E/AndroidRuntime(417): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
07-16 04:02:28.728: E/AndroidRuntime(417): at org.json.JSONTokener.nextValue(JSONTokener.java:90)
07-16 04:02:28.728: E/AndroidRuntime(417): at org.json.JSONArray.<init>(JSONArray.java:87)
07-16 04:02:28.728: E/AndroidRuntime(417): at org.json.JSONArray.<init>(JSONArray.java:103)
07-16 04:02:28.728: E/AndroidRuntime(417): at com.database_demo.Database_demo.onCreate(Database_demo.java:78)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-16 04:02:28.728: E/AndroidRuntime(417): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-16 04:02:28.728: E/AndroidRuntime(417): ... 11 more
解决方法:
从以下位置更改此行:
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
对此:
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"), 8192);
它应该删除该消息