我正在将我的应用程序连接到我的服务器
以下是我访问互联网的表现文件
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
它在API级别10中工作
但是当涉及到套件kat版本的API级别19时,它无法访问互联网,它会抛出异常。
我是否需要添加任何其他权限
这是我用来连接我服务器的代码
public class Discussion_server extends AsyncTask<String,Void,JSONObject> {
static String jsonstring="";
private Handler handler = new Handler();
private ProgressDialog dialog;
private Activity activity;
public Discussion_server(Activity activity)
{
Log.d("progressing","progressing");
this.activity = activity;
this.dialog = new ProgressDialog(activity);
// onPreExecute();
dialog.setMessage("Contacting Remote Site");
dialog.show();
handler.postDelayed(new Runnable()
{
@Override
public void run() {
onExecute();
}
},3000);
}
protected void onExecute(){
dialog.dismiss();
}
@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
try{
Log.d("boooooo","beeeee");
// Toast.makeText(this,comment,Toast.LENGTH_LONG).show();
String link="http://www.diveintojava.com/Discussion/mobile_discussion.php";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line=null;
jsonstring="";
while((line = reader.readLine()) != null)
{
Log.d("na","na");
jsonstring+=(line+ "\n");
//break;
}
//onPostExecute();
return new JSONObject(jsonstring);
}catch(Exception e){
Log.d("veaadsldsld","sdlkfsdlfsldklfsd");
Log.d("exc",e.getMessage());
//Toast.makeText(activity,"Connection timed out!!",Toast.LENGTH_LONG).show();
return null;
}
}
}
这是我的logcat显示的内容
07-10 02:23:39.827: E/AndroidRuntime(838): FATAL EXCEPTION: main
07-10 02:23:39.827: E/AndroidRuntime(838): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.diveintojava1/com.example.diveintojava1.Discussion}: java.lang.NullPointerException: println needs a message
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.os.Looper.loop(Looper.java:137)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 02:23:39.827: E/AndroidRuntime(838): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 02:23:39.827: E/AndroidRuntime(838): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 02:23:39.827: E/AndroidRuntime(838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 02:23:39.827: E/AndroidRuntime(838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 02:23:39.827: E/AndroidRuntime(838): at dalvik.system.NativeStart.main(Native Method)
07-10 02:23:39.827: E/AndroidRuntime(838): Caused by: java.lang.NullPointerException: println needs a message
07-10 02:23:39.827: E/AndroidRuntime(838): at android.util.Log.println_native(Native Method)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.util.Log.d(Log.java:138)
07-10 02:23:39.827: E/AndroidRuntime(838): at com.example.diveintojava1.Discussion_server.doInBackground(Discussion_server.java:100)
07-10 02:23:39.827: E/AndroidRuntime(838): at com.example.diveintojava1.Discussion.onCreate(Discussion.java:78)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.Activity.performCreate(Activity.java:5104)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-10 02:23:39.827: E/AndroidRuntime(838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-10 02:23:39.827: E/AndroidRuntime(838): ... 11 more
答案 0 :(得分:0)
使用过的网址&#34; http://www.diveintojava.com/Discussion/&#34;给出html输出。在您使用&#34; http://www.diveintojava.com/Discussion/mobile_discussion.php&#34;之前这给了json文本。你需要json解析器的json文本。