Posting variables with Android - php

I use my code to upload data in MySQL.
HttpPost httppost = new HttpPost(URL_POST_TIENDAS);
HttpClient client = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", json));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
// Log.e(TAG, "Ejecutando POST: Mandando tiendas");
HttpResponse httpResponse = client.execute(httppost);
if (httpResponse != null) {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
message = NetworkUtils.Entity2String(httpResponse);
Log.e(TAG, "Respuesta del Post Tienda:" + message);
}
} else {
Err error = new Err(statusCode, message, "upload_tiendas");
MyApplication.lErrors.add(error);
this.cancel(true);
}
This code give me a 500 Error
In PHP, I receive my variable with $_REQUEST, so when I debug my app, copy json variable and put it in the full URL, there is no problem.
This show my json variable is OK, as URL_POST_TIENDAS.
Why is there a problem with using POST??? This is not the first time I find this problem.
I always change it to GET, but this time, I want to understand why it fails, because I could have a lot of information to upload, so GET is not very appropriated!
EDIT : When seeing logs server, I don't see anything about my 500 error.
EDIT2: httpost :
httppost HttpPost (id=830032727152)
aborted false
abortLock ReentrantLock (id=830032727328)
connRequest null
entity UrlEncodedFormEntity (id=830032731528)
chunked false
content (id=830032746160)
[0...99]
[100...199]
[200...299]
[300...399]
[400...499]
[500...599]
[600...699]
[700...724]
contentEncoding null
contentType BasicHeader (id=830032747320)
headergroup HeaderGroup (id=830032727200)
headers ArrayList (id=830032727216)
array Object[16] (id=830032727240)
modCount 0
size 0
params BasicHttpParams (id=830032789608)
parameters null
releaseTrigger SingleClientConnManager$ConnAdapter (id=830032798576)
uri URI (id=830032727376)
Any Help will be appreciated !

Set content type in your httpPost
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
Update
Here is the blog which is doing the same thing.
Send data as json from android to a PHP server

You should use $_POST array instead of $_REQUEST when you are using POST method for sending params.
I think this will resolve your problem.

Related

Why is it my PHP Webservice cannot receive data from my Android device?

Both my laptop and my Android device is connected to the same WiFi. and I am trying to send data from my phone to the PHP webservice. But I can't get it work. What is the problem?
IP of my laptop: 192.168.0.10
IP of my phone: 192.168.0.9
I am listening from port80: ie:
HttpPost httppost = new HttpPost("http://192.168.0.10:80");
Following are the code of my PHP file:
<?php
$get = json_encode($_POST["req"]);
// Get data from object
$name = $get->req; // Get name you send
$age = $get->req; // Get age of user
?>
and below are my android code. Which part i'm doing wrongly? Please help!
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.10:80");
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("name", "Jensen");
jsonobj.put("age", "22");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("req", jsonobj.toString()));
Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
// Use UrlEncodedFormEntity to send in proper format which we need
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Another thing, I have tried sending the data over too. I will need to press a button then the data will be passed. After I press the button, this is what I got (I'm connecting my phone through ADB):

Getting HTML response instead of JSON in android

I'm trying to retrieve data from mysql database located on a remote server through php into android. This worked fine while running in localhost. But when it is in remote server, I receive HTML codes instead of JSON response from server. I tried pasting the url (http://ksos.0fees.us/pgm_list.php?day=1&hall=A) in browser which resulted in correct JSON output. The HTML response is shown below.
<html><body>
<script type="text/javascript" src="/aes.js" ></script>
<script>function toNumbers(d)
{var e=[];
d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});
return e
}
function toHex()
{for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)
e+=(16>d[f]?"0":"")+d[f].toString(16);
return e.toLowerCase()
}
var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("5cb1c0309e553acda177d912f21ac485");
document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+";
expires=Thu, 31-Dec-37 23:55:55 GMT;
path=/";
location.href="http://ksos.0fees.us/pgm_list.php?day=1&hall=A&ckattempt=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
</body></html>
Following is my request to server for getting response
public String makeServiceCall(String url, int method, List<NameValuePair> params){
try{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpEntity httpentity = null;
HttpResponse httpresponse = null;
if (method == POST) {
HttpPost httppost = new HttpPost(url);
httppost.setHeader("User-Agent", ua);
httppost.setHeader("Accept", "application/json");
if(params!=null){
httppost.setEntity(new UrlEncodedFormEntity(params));
}
httpresponse = httpclient.execute(httppost);
} else if(method == GET){
if(params!=null){
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpget = new HttpGet(url);
// HttpPost httppost = new HttpPost(url);
httpget.setHeader("User-Agent", ua);
httpget.setHeader("Accept", "application/json");
httpresponse = httpclient.execute(httpget);
}
httpentity = httpresponse.getEntity();
is = httpentity.getContent();
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch (ClientProtocolException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
// BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine())!= null) {
sb.append(line+"\n");
}
is.close();
response = sb.toString();
}catch(Exception e){
Log.e("Buffer Error", "Error : "+e.toString());
}
return response;
}
I've tried by setting and not setting setHeader for user agent.
The php part looks as follows:
$q=mysql_query("SELECT ...........");
if(!empty($q)){
while($row=mysql_fetch_assoc($q))
$pgmlist[]=$row;
$response["pgmlist"] = $pgmlist;
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "No record found";
echo json_encode($response);
}
Atlast found solution...
The issue was not with the android or php part. It was the problem with the server. The hosting site which I used, sends cookies to client side which is not handled inside android but is automatically handled by browsers. I used to another hosting site where cookies are not involved and got the needed json output.
I spent a long time to understand and solve the problem.
Firstly, we need to understand that 0fess hosting has anti bot technique which blocks the calls from (none-browser) clients. The main idea of this technique is using a javascript script that checks if the request is coming from a normal web browser then the script encrypts the IP and sets a cookie with key __test and value of the encrypted IP.
To solve such a problem we need to run a webview inside our application and request any page from the our 0fees site. then we can intercept the response and get the cookie. after that, we can use this cookie as a request header in our http rest requests.
This is a sample code
WebView browser=new WebView(getContext());
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url){
final String cookies = CookieManager.getInstance().getCookie(url);
Log.d("any", "All the cookies by me in a string:" + cookies);
HttpPost httppost = new HttpPost("http://ksos.0fees.us/pgm_list.php");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Cookie", cookies);
//continue your request paramters
}
}
);
browser.loadUrl("http://ksos.0fees.us/");

Uploading file using multipartformentity to php script (file ends up in $_POST)

I have an android app which uses the multipartentity from apache httpcomponents. The app uploads a file using an HTTP request to a php script. However, the file that I am uploading (.ogg) ends up in $_POST instead of $_FILES and it looks like binary, although I could be wrong.
I have the chrome rest extension and I used it to POST a multipart/form-data request with the exact same .ogg file and the file ends up in the correct spot ($_FILES).
Android code is as follows:
// Send the file to the server.
// Create HTTP objects.
// Request.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ACTION);
// Response.
HttpResponse httpResponse;
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentBody cbFile = new FileBody(mediaFile, "audio/ogg");
mpEntity.addPart("file", cbFile);
httpPost.setEntity(mpEntity);
// Execute.
httpResponse = httpClient.execute(httpPost);
// Evaluate the response.
int statusCode = httpResponse.getStatusLine().getStatusCode();
// Make sure everythings okay.
if (statusCode == 200) {
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
String body = EntityUtils.toString(resEntity);
resEntity.consumeContent();
Log.d("APP_STATUS", body);
}
}
httpClient.getConnectionManager().shutdown();
And the very simple PHP script:
<?php
echo "\$_POST";
var_dump($_POST);
echo "\$_FILES";
var_dump($_FILES);
?>
Response when using the app:
Response when using the Chrome REST extension:
If anyone has any insight to what I'm failing epicly at, please let me know.
Try using the entity builder
HttpEntity mpEntity = MultipartEntityBuilder.create()
.addBinaryBody(
"file", cbFile, ContentType.create("audio/ogg"), cbFile.getName())
.build();

Array content goes missing in transit from Android to PHP via HttpPost

I am trying to send an array from Android to PHP. The array is a basic key-value pair (string, string) which consists of all the contacts from my device phonebook (name, phone_number). Here's the code snippet that does this:
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
int i=0;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for(i=0;i<item.size();i++) {
nameValuePairs.add(new BasicNameValuePair(item.get(i).getName(), item.get(i).getNumber()));
}
Log.v("Count", ""+i);
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v("TAG", "Response: " + responseStr);
}
} catch (IOException e) {
e.printStackTrace();
}
On PHP end, the code is meant to accept the data in POST and echo it out. The code goes:
<?php
// Run script only if the dump array is received
if($_POST){
echo "server received: ".count($_POST);
// Read comma-separated text file
$arr = 0;
foreach($_POST as $names[$arr] => $numbers[$arr]) {
$numbers[$arr] = preg_replace('/[^0-9]/','',$numbers[$arr]);
$arr += 1;
}
for($i=0;$i<$arr;++$i) {
echo $i.". ".$numbers[$i]."-------".$names[$i]."\n";}
}
?>
This works perfect for smaller phonebooks. However, while running this on a phonebook with exactly 100 phone number entries, the echo dump shows only 87. What gives? I even tried using GET and REQUEST instead of POST but results remain the same.
It looks like issues is in form content type, which is application/x-www-form-urlencoded by default. Here is more information on form content types:
The content type "application/x-www-form-urlencoded" is inefficient
for sending large quantities of binary data or text containing
non-ASCII characters. The content type "multipart/form-data" should be
used for submitting forms that contain files, non-ASCII data, and
binary data.
Take a look here on how to use multipart/form-data instead:
Post multipart request with Android SDK

Error sending parameters to php

when connecting android to php sometimes parameters not sent.
user=i.getExtras().getString("user");
params.add(new BasicNameValuePair("user",user));
// getting JSON string from URL
json = jParser.makeHttpRequest(url_orders, "GET", params);
result obtained based on empty user.
This worked for me try this :
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://URL.com.php");
postParameters.add(new BasicNameValuePair("param1", value1));
postParameters.add(new BasicNameValuePair("param2", value2));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent())
.toString()

Categories