android httprequest error only in tablet - php

Hello I have an android application inserting data to mysql database with php. My insert code works perfect on Avd and my android phone. But it gives error on tablet. My tablet is connected to wireless internet and other parts of application works fine and getting data from json. What is the difference between devices about this error?
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mysite.com/android3.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e){
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "error error error", Toast.LENGTH_LONG).show();
}

It might be because of : http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
This was introduced in API 11 and android allows network calls on main thread in previous versions without throwing an error.

Related

Retrieve data from android app to PHP website

Is there any method to get data from an Android app to a website?
If it is from website to website, with "file_get_contents" it is possible. But then any idea on getting data from Android apps?
Thanks in advance!
Using this simple php script you can get data from android:
<?php
$filename="datatest.html";
file_put_contents($filename,$_POST["fname"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fphone"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["femail"]."<br />",FILE_APPEND);
file_put_contents($filename,$_POST["fcomment"]."<br />",FILE_APPEND);
$msg=file_get_contents($filename);
echo $msg; ?>
In android can use HttpPost to accomplish this by something like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/mypage.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("fname", "jake"));
nameValuePairs.add(new BasicNameValuePair("fphone", "9999999"));
nameValuePairs.add(new BasicNameValuePair("femail", "xyz#live.com"));
nameValuePairs.add(new BasicNameValuePair("fcomment", "Help"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
Replace your data with the fields you want to set and send in the constructor of BasicNameValuePair.
Here is the actual Source
You could execute post requests in android to your website. Although this has to executed from a different thread, since android doesn't allow network operations on the main thread.

insert data to local host xampp

my app is working perfect when i am saving data to web server,but when i save it on local host database it is giving
Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject error..
here is my code:
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.website.com/exmpl/insertData.php"); //working perfectly
HttpPost httppost = new HttpPost("http://192.168.0.1/exmpl/insertData.php");
httppost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
log cat:
01-05 19:02:53.150: E/pass 1(28604): connection success
01-05 19:02:53.410: E/Fail 3(28604): org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
EDIT
errors if i change the ip address to 192.168.0.123
01-06 10:00:37.560: E/Fail 1(4261): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.123 refused
01-06 10:00:37.570: E/AndroidRuntime(4261): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
and i am not using async task,,i am extending activity
even tried 192.168.0.123:80 followed MR.HU'NG
Error again
Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
The return value of 192.168.0.1/exmpl/insertData.php on the ANDROID DEVICE are not the same of your PC, because, 192.168.0.1 in the DEVICE points to itself, not to you apache server.
You must use the IP of your machine, assigned by the local router, or configure your emulator to use some fixed ip.
Probably, the 192.168.0.1/exmpl/insertData.php are returning something like "page not found".

Android to PHP error- Unidentified Index

when sending data to a php server through android I cam across this error: "Notice: Undefined index: IT in C:\xampp\htdocs\hello.php on line 4". After a few hours of messing around with it I can't solve my issue.
hello.php :
<?php
mysql_connect("localhost","user","pass");
mysql_select_db("mmo");
$r=mysql_query("update players set X = '".$_REQUEST['IT']."' where 22=22");
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
header('Refresh: 0.01; URL=http://localhost/hello.php');
?>
update method in android:
public void UpdateSeverWithPlayer()
{List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("IT","3"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/hello.php");
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());
}}
I am working on the android emulator and do have the internet permission. As always help is greatly appreciated.
EDIT 1: It appears the issue is in the android and not the php code.
You're not sending your query string propertly somehow, so there is no $_REQUEST['IT'] on the PHP side. do a var_dump($_REQUEST) to see what's coming through. Or better yet, don't use $_REQUEST. It's somewhat insecure. Better use the superglobal directly related to your HTTP method, _GET or _POST.
As well, the Refresh header is non-standard. it'll work, but you shouldn't depend on it. An HTTP level redirect uses the Location: header. Refresh: is a non-standard old-school Netscape extension.

Android HTTPPost Null

I am trying to post a string to a php file on my web server from my android phone. I am able to establish a connection to the file, as I can retrieve what it prints. The problem I am having is sending the POST variable, which continually seems to be null(I am testing the program now, and sending back what the POST variable is). Here is the relevant code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(HTTP://WWW....PHP FILE ADDRESS);
try{
// the data to send
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("RoomName", "HELLO"));//params[1]
// http post
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
My PHP code is (at the moment) as follows:
$roomname = $_POST['RoomName'];
$returnArray[] = array("LoggedIn" => $roomname);
print_r(json_encode($returnArray));
I have been looking at forums for the past week, and all the httppost examples follow this almost exactly. I would appreciate any help that you are willing to give. Thanks a lot
Well, try $_REQUEST in place $_POST.

Android- UnknownHostException stumper

i have been trying to call a PHP script with the android system. i am using Eclipse helio and everything else i have written works fine on it - not much yet. so i block copied this code and it does not work: i have no warnings or errors and i can debug and step through it, but it always comes back with
"E log_tag : Error in http connection java.net.UnknownHostException: www.X.com"
here is the code:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Lat","19.80"));
nameValuePairs.add(new BasicNameValuePair("Lon","13.22"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.X.com/new/getAllPeopleBornAfter.php");
try{
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
and the PHP which runs fine if i go there with a browser:
mysql_connect("mysql27.x.com","name","PW");
mysql_select_db("dbname");
$q = mysql_query("SELECT * FROM people");
while ($e = mysql_fetch_assoc($q)) $output[] = $e;
mysql_close();
echo (json_encode($output));
?>
if you guys want me to run anything in a different format or any more info - please let me know - i cant find any thing more than just unknownhostexception
Can you access www.X.com from the emulator/phone?
Do you have the INTERNET permission?
Did you remember to put:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
into your manifest file?
Are you behind a proxy network? Should you be setting the proxy ip address and port number? I'm no good with php. But I get this error when I try to connect to the internet through java programs. The solution works good for me.

Categories