-1

1) implement web service in netbeans with json which fetch data from MySQL database

2) create rest client in android to call web service

Edited code

my manifest file

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

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

        <activity
            android:name=".MainActivity"
            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>

jsonParse.java

package android.project.srt.demojson;

public class JsonParser {

 static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JsonParser() {
}

public String getJSONFromUrl(String url) {
    // Making HTTP request
    try {
        HttpGet get = new HttpGet(url);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();

        InputStream inputStream=entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream),8 * 1024);

        String line=null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    inputStream.close();
    } catch (UnsupportedEncodingException e) {
        Log.e("unsupported ","encoding exception" +e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        Log.e("clientprotocolexception ","" + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e("io","Exception " + e.getMessage());
        e.printStackTrace();
    }

    return sb.toString();
}
}

MainActivity.java

public class MainActivity extends ActionBarActivity {

private static String url = "http://192.168.1.31:8084/adminWebservice/mywebservice/SelectRestroTypes";
String categoryname;
TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView1);

    //new JSONParse().execute();

    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Loading Data ...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
    tv1.setText("Getting Values Pls wait..");

    JsonParser jParser = new JsonParser();
    // Getting JSON from URL
//    JSONObject json = jParser.getJSONFromUrl(url);

    String jsonString=jParser.getJSONFromUrl(url);
    tv1.setText(jsonString);
}    
}

logcat shows this only error :

03-24 19:11:20.914  30629-30629/android.project.srt.demojson E/ViewRootImpl﹕ sendUserActionEvent() mView == null

any suggestions regarding creating json client in android are welcome

thanks in advance for help

Shruti
  • 391
  • 1
  • 5
  • 21
  • which line is this one? `MainActivity.java:114` – eduyayo Mar 24 '15 at 09:15
  • is not related with your code but a general advice, you should use Retrofit to have a nice REST API in your android app. It's powerfull, well-tested, and straight forward. http://square.github.io/retrofit/ – Hugo Gresse Mar 24 '15 at 09:19
  • 114 line : categoryname = json.getString("CategoryName"); – Shruti Mar 24 '15 at 09:26
  • @Hugo Gresse , thanks , will also try Retrofit – Shruti Mar 24 '15 at 09:26
  • are you blind? `...http://localhost:8084 refused...` or you have a problems with reading? is the server on device/emulator? – Selvin Mar 24 '15 at 09:30
  • Look at my code @Selvin , In main activity.java i have used url this "192.168.1.31:8084/adminWebservice/mywebservice/... " not localhost – Shruti Mar 24 '15 at 13:30
  • look at the logcat logs ... re-build and re-install the app on the device/emulator – Selvin Mar 24 '15 at 13:32

1 Answers1

0

Seems like this is your problem

Exception Connection to http://localhost:8084 refused

  • The url should point to the server address where your webservices are running instead of localhost also the port 8084 should be open if there is a firewall inbetween. – Sameer Acharya Mar 24 '15 at 11:58
  • look at my code my URL in MainActivity.java is : "http://192.168.1.31:8084/adminWebservice/mywebservice/SelectRestroTypes" – Shruti Mar 24 '15 at 12:33
  • I dont see the old error now seems like you have edited the question, the error seen now is different, have you tried searching the web I see a lot of links with this issue [link]http://stackoverflow.com/questions/18028666/senduseractionevent-is-null – Sameer Acharya Mar 25 '15 at 09:15
  • ok got it, cos I run the app in samsung s3 , and its a bug , thanks @Sameer – Shruti Mar 25 '15 at 12:45