Reputation: 381
On the PHP end, I'm getting a JSON Object that we're sending back to Android. The PHP code is straightforward and just echos the JSON object like I have seen in almost every tutorial.
This line creates an exception every other query. Am I doing something very wrong?
JSONObject json = jsonParser.makeHttpRequest(signInURL,
"POST", params1);
04-15 21:40:44.787: E/AndroidRuntime(6325): java.lang.RuntimeException: An error occured while executing doInBackground()
I'm following these instructions:
How json will be used in our application.
In our Android application,
Edit: The problem was actually just PHP warnings that needed to be fixed. Thanks everyone!
Upvotes: 0
Views: 730
Reputation: 800
set internet permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Http request from android to server
public String httpRequest(String url, String query, int methodtype){
try {
String reqUrl = url + query;
switch (methodtype) {
case 1:
HttpGet httpGet = new HttpGet(reqUrl);
httpResponse = httpClient.execute(httpGet);
break;
case 2:
HttpPost httpPost = new HttpPost(reqUrl);
httpResponse = httpClient.execute(httpPost);
break;
}
HttpEntity httpEntity = httpResponse.getEntity();
instrObj = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
//HandleException
} catch (ClientProtocolException e) {
//HandleException
} catch (IOException e) {
//HandleException
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(instrObj, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
instrObj.close();
json = sb.toString();
} catch (Exception e) {
}
return json;
}
For posting the form data to the server HttpPost is used and to retreive the data from server HttpGet is used....
Upvotes: 3
Reputation: 637
Try this piece of code
try {
URL Url = new URL(" --------");
HttpURLConnection connection = (HttpURLConnection) Url.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
JSONObject jsonResponse = new JSONObject(charArray.toString());
}
else {
Log.i("", "HTTP Response Code: " + responseCode);
}
}
catch (Exception e) {
}
Upvotes: 1
Reputation: 11197
It sometimes happens for not setting the internet permission.
To set internet permission do this:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Hope this helps.. :)
Upvotes: 1