0

I want to develop an android application which fetches data from web server (my free domain account on www.byethost.com) and displays it on my android application in a List view. The problem which i faced from last 5 days is that when i run my PHP script on localhost(WAMP) and call this from emulator using this link http://10.0.2.2/demo.php it runs fine.But when i place this script on free domain panel and call from this link http://zamin.byethost14.com/demo.php it doesnot show anything on my real device which is connected to my PC using USB cable. Since last 5 days i have searched many links but it doesnot show any information which is relevant to my problem. Thanking you in Advance.

MainActivity.java

package com.example.abdul.zx;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends Activity
{

String myJSON ;
String id;
String name;
private static final String TAG_RESULTS="result";
// private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// private static final String TAG_ADD ="address";

JSONArray peoples = null;

ArrayList <HashMap <String, String> > personList;

ListView list;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.listView);
    personList = new ArrayList<HashMap<String,String>>();
    getData();


}


protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
           // id = c.getString(TAG_ID);
            name = c.getString(TAG_NAME);
           // String address = c.getString(TAG_ADD);

            HashMap<String,String> persons = new HashMap<String,String>();

         //   persons.put(TAG_ID,id);
            persons.put(TAG_NAME,name);
          //  persons.put(TAG_ADD,address);

            personList.add(persons);
        }

        final ListAdapter adapter = new SimpleAdapter
                (MainActivity.this, personList, R.layout.list_item,
                        new String[]{TAG_NAME},
                        new int[]{R.id.name});

        list.setAdapter(adapter);
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               switch(i)
               {
               case 0 :
               Intent appInfo = new Intent(MainActivity.this, Desserts.class);
               startActivity(appInfo);

               break;
               case 1 :
               Intent ap = new Intent(MainActivity.this, MainActivity3.class);
               startActivity(ap);
               break;
               case 2 :
               Intent Info = new Intent(MainActivity.this, Desserts.class);
               startActivity(Info);
               break;
           }}
       });
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            //HttpPost httppost = new HttpPost("http://10.0.2.2/ch.php");
            HttpPost httppost = new HttpPost("http://zamin.byethost14.com/demo.php");
            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }

            return result;

        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}



}

demo.php

<?php
mysql_connect("sql206.byethost14.com","username","password") or  die(mysql_error());
mysql_select_db("database name");
$sql=mysql_query("select * from demo");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

logcat

PropertyFetcher: AdbCommandRejectedException getting properties for device   LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems   wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device  LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems  wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device   LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems  wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device   LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems  wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device   LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems  wrong.
Otherwise check for a confirmation dialog on your device.
PropertyFetcher: AdbCommandRejectedException getting properties for device  LC524YE42425: device unauthorized.
This adbd's $ADB_VENDOR_KEYS is not set; try 'adb kill-server' if that seems  wrong.
Otherwise check for a confirmation dialog on your device.

1 Answers1

0

I tried accessing the the API link from the browser. Are you sure it is a HTTP Post request? trying this

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://zamin.byethost14.com/demo.php");
request.setURI(website);
HttpResponse response = httpclient.execute(request);
crashOveride
  • 839
  • 6
  • 12
  • When i change POST to GET, it shows this exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference on this line JSONObject jsonObj = new JSONObject(myJSON); – Default Code Mar 22 '16 at 13:43
  • Your response format is JSONArray. try this JSONArray jsonObj = new JSONArray(myJSON); – crashOveride Mar 22 '16 at 17:23
  • i have changed this, but no solution still same problem – Default Code Mar 23 '16 at 07:30
  • myJson is null. It is not getting updated. Confirm this my logging it before JSONObject jsonObj = new JSONObject(myJSON). is it because you are trying modify a class variable from within an inner class? – crashOveride Mar 23 '16 at 10:24
  • Try passing myJSon from AsyncTask to Activity in a better way. Make a constructor in AsyncTask which Activity object. And now update the value of myJson by doing myActivity.myjson = result; – crashOveride Mar 23 '16 at 10:30
  • Refer : http://stackoverflow.com/questions/9447646/how-do-i-send-data-back-from-onpostexecute-in-an-asynctask#answer-9447833 MainActivity.this.MyJson = result; – crashOveride Mar 23 '16 at 12:29
  • i used byethost free domain. Is it a problem? – Default Code Mar 23 '16 at 13:23