Error in http connection

hi I'm trying to extract data from a link as below

http://abovestress.com/app_stress/fetch_all_detail.php?task=fetchtimefromdateanduserid&track_date=2011-08-09&tracker_user_id=374 

but I can’t get the result, my code is here

 package com.JsonDemo; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; 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.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class JsonDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList<String> fetchsosfromID = new ArrayList<String>(); String result = ""; InputStream is = null; ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("track_date","2011-08-09")); nameValuePairs.add(new BasicNameValuePair("tracker_user_id",""+374)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://abovestress.com/app_stress/fetch_all_detail.php?task=fetchtimefromdateanduserid&"); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } // convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); Log.v("log_tag", "Append String " + result); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } // parse json data try { JSONArray jArray = new JSONArray(result); for (int i = 0; i < jArray.length(); i++) { JSONObject json_data = jArray.getJSONObject(i); fetchsosfromID.add(json_data.getString("track_time")); Log.v("log_tag", "daily_data " + fetchsosfromID); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } 

}

and the error occurs as

 08-09 17:29:01.315: ERROR/log_tag(2291): Error in http connection java.net.UnknownHostException: abovestress.com 08-09 17:29:01.315: ERROR/log_tag(2291): Error converting result java.lang.NullPointerException 08-09 17:29:01.345: ERROR/log_tag(2291): Error parsing data org.json.JSONException: End of input at character 0 of 

Explicit

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.JsonDemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".JsonDemoActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-permission android:name="android.permission.INTERNET"/> </application> </manifest> 
0
source share
3 answers

you need to put the tag tag permission of the application tag ...

+2
source

You definitely added

 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

in your manifest, right?

Edit: Your <uses-permission> is in the wrong place. It should look like this:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.JsonDemo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".JsonDemoActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
+4
source

The exception you receive indicates a name resolution problem (getting the IP address abovestress.com from the DNS server). This is probably due to the fact that your network is down.

A simple snippet to check if a network connection is enabled, where Context ctx is the action context:

 public boolean checkConnection(Context ctx) { ConnectivityManager conMgr = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo i = conMgr.getActiveNetworkInfo(); if (i == null) return false; if (!i.isConnected()) return false; if (!i.isAvailable()) return false; return true; } 

EDIT:

If the network is not your problem, look here and here.

you may need to add (besides android.permission.INTERNET ) other permissions:

 android.permission.ACCESS_NETWORK_STATE android.permission.READ_PHONE_STATE 

and / or

 try { InetAddress i = InetAddress.getByName(URLName); } catch (UnknownHostException e1) { e1.printStackTrace(); } // ... actually using URL 

EDIT 2: And as others have noted, the uses-permission element is inside the manifest element, not the application element

0
source

Source: https://habr.com/ru/post/1413334/


All Articles