0

hi I am trying to fetch data from link as given 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 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 error comes like this

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 

manifiest

<?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>

3 Answers 3

4

You've definitely added the

<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>
Sign up to request clarification or add additional context in comments.

2 Comments

It is a manifest problem. the <uses-permission> should be outside the <application> tag.
@Jignesh Ansodariya: Could you please stop using twenty thousand dots? And if you comment hasn't enough characters, than simple form a sentence. Thank you.
2

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

Comments

0

The exception you get points to a name resolution problem (getting abovestress.com ip address from DNS server). That is probably because your network is down.

A simple snippet to check if a network connection is up, where Context ctx is you Activity 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 network in not your problem, have a 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 noted by others, uses-permission element goes inside manifest element, not application element

2 Comments

No, network is ok because other applications are working on Json
ok, take a look at my edited answer for possible alternatives to workaround hostname resolution problems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.