I have some problems with Receiving PHP Echo message from an website.
I have this code:
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://www.itbstudios.tk/test.php");
HttpResponse response = httpclient.execute(httppost);
String str = EntityUtils.toString(response.getEntity());
I found this code and a lot more here on stackoverflow and is is not working. The app is crahsing on httpclient.execute();
Internet permission is set in android manifest.
Php code is just an <?php echo "test"; ?>
I'm building for API 15 and testing on HTC Device.
Can you guys help me?
I even created an other project and paste the code in on create and i have the same problem.
Thanks!
HttpClient got some bugs in API higher than 10. Use HttpUrlConnection instead.
GET method example:
URL url = new URL("http://www.itbstudios.tk/test.php");
HttpUrlConnection mUrlConnection = (HttpURLConnection) url.openConnection();
mUrlConnection.setDoInput(true);
InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
String s = readStream(is);
Related
I want to using HttpPost to post data to my web site.
and here is my code
String st = "http://www.yade.cc/huli/classget/search.php";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(st);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("number", str));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse response = client.execute(post);
String retSrc = EntityUtils.toString(response.getEntity(),"utf-8");
but the result is:
406 Not Acceptable
An appropriate representation of the requested resource /huli/classget/search.php could
not be found on this server
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument
to handle the request.
when I add user-agent data into mycode, it works.
post.setHeader("User-Agent", "Chrome/28.0.1500.95");
but the result is something like
口口口口口口result口口口口
口means the char i can not see and i dont know what it is.
I try to use php to do the same thing, the result is totally ok, nothing strange appeared.
how to solve this problem?
I send a file via HTTPPost in android:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
In PHP how do I get the file?
I'm not really sure where your file would end up on the server. As far as I can tell you won't be able to access it using the $_FILES array.
I would suggest that you try to upload the file to your server the same way as it would be uploaded using an HTML form. If you do it that way you can handle the file upload using the $_FILES array. Judging from similar questions on SO there's a ton of code you need to write to get this to work though... BUT Android Asynchronous Http Client has good support for file uploads out of the box:
AsyncHttpClient client = new AsyncHttpClient();
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
client.post("http://www.yourserver.com/upload.php", params, responseHandler);
}
catch(FileNotFoundException e) {
// handle
}
I am trying to output POST-data that I send from an android phone, however I cannot manage to get any output in my PHP file.
This is my php code:
<?php
echo "POSTed data: '".$_POST['mydata']."'<br>";
var_dump($_POST);
?>
this is my android code (which seems to work fine)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("myurl.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("mydata", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringData", "SomeData"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String responseText = EntityUtils.toString(response.getEntity());
toSendET.setText(responseText);
Is it even possible to send POST data to an static PHP script via phone?
What you have got is working fine - but when you echo something in PHP its echo'd back to the caller - ie the browser in this case.
If you check the value of response in the code on Android is will be POSTed data: '12344'<br>
See the error_log() function in PHP for logging to a file
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.
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.