December 26, 2014

Sending Parameters to URL or web service in POST Method

Below code snippet explains how to send parameters to a web url in the POST method.
                               
URL url = new URL(“http://testulr/searchuser”);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoOutput(true);
String params=”userid=”+userid+”&username=”+name;
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(params);
writer.flush();
out.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder st = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
                st.append(line);
}
System.out.println("Result: "+st);
urlConnection.disconnect();
  
If the url is secured then replace HttpURLConnection class with HttpsURLConnection in the above code.

December 11, 2014

R.java file is missing in android application

While working with an android application in eclipse, some times the R.java file may missed out from the application. This is because the XML files in the app may have errors. So, resolve those errors in the XML files and then clean the project. Now the R.java file will be automatically created.

November 20, 2014

android.content.ActivityNotFoundException: Unable to find explicit activity class

I am developing an android application. On the home screen, it has a button, on click of this an activity should be displayed. While testing the app, on click of the button, the app is giving the below error and the app got terminated.

E/AndroidRuntime(Number): java.lang.RuntimeException: Unable to resume activity {app package}: android.content.ActivityNotFoundException: Unable to find explicit activity class {activity class}; have you declared this activity in your AndroidManifest.xml?

The solution for this is,

Every activity created in android application should be configured in AndroidManifest.xml of the app. To do so, open AndroidManifest.xml and goto Application tab. Under Application Nodes, add all the activities.