Tuesday, 15 January 2013

Send JSON from Java to PHP through Post -



Send JSON from Java to PHP through Post -

i doing android programme supposed send info tablet php web service. code sending json:

package com.example.shvalidation; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicheader; import org.apache.http.protocol.http; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.view; public class mainmenuscreen extends activity { //json variables jsonparser jsonparser = new jsonparser(); string pid; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main_menu_layout); new testthread().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main_menu_layout, menu); homecoming true; } public void planttodome(view view) { intent intent = new intent(this, selectlocationscreen.class); startactivity(intent); } //código del web service public class testthread extends asynctask<void, void, void> { progressdialog dialog; protected void onpreexecute() { dialog = progressdialog.show(mainmenuscreen.this, "loading", "loading data, please wait.."); } private string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; seek { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } grab (ioexception e) { e.printstacktrace(); } { seek { is.close(); } grab (ioexception e) { e.printstacktrace(); } } homecoming sb.tostring(); } protected void doinbackground(void...args0) { seek { httpclient client = new defaulthttpclient(); httpresponse response; httppost post = new httppost("http://192.168.1.101:8080/getbook.php"); jsonobject holder = new jsonobject(); jsonobject euid = new jsonobject(); euid.put("euid", 1); holder.accumulate("euids", euid); euid.put("euid", 2); holder.accumulate("euids", euid); post.setheader("json", holder.tostring()); stringentity se = new stringentity(holder.tostring()); se.setcontentencoding(new basicheader(http.content_type, "application/json")); post.setentity(se); response = client.execute(post); if (response != null) { inputstream in = response.getentity().getcontent(); string = convertstreamtostring(in); log.i("read server", a); } } grab (exception e) { log.d("error", e.tostring()); } homecoming null; } protected void onpostexecute(void unused) { dialog.dismiss(); } } }

the php web service:

<?php ob_start(); var_dump(json_decode(file_get_contents('php://input'))); $out = ob_get_contents(); ob_end_clean(); $f = fopen('out.txt', 'w+'); fwrite($f, html_entity_decode($out)); fclose($f); ?>

i have tried different methods getting json, none of them have worked me. maybe fine people of stackoverflow can help me out this, have every other problem i've had.

from comments section, appears want json beingness sent php script. normally, post post php, , extract it:

<?php print_r($_post); $json_string = $_post['message']; $json = json_decode($json_string); print_r($json); ?>

and little client example:

public static void main(string[] args) { string json = "{\"message\":\"this message\"}"; httpclient httpclient = new defaulthttpclient(); seek { httppost request = new httppost("http://somesite.com/test.php"); stringentity params =new stringentity("message=" + json); request.addheader("content-type", "application/x-www-form-urlencoded"); request.setentity(params); httpresponse response = httpclient.execute(request); // handle response here... system.out.println(org.apache.http.util.entityutils.tostring(response.getentity())); org.apache.http.util.entityutils.consume(response.getentity()); } grab (exception ex) { // handle exception here } { httpclient.getconnectionmanager().shutdown(); } }

the output of is:

array ( [message] => {"message":"this message"} ) stdclass object ( [message] => message )

java php android

No comments:

Post a Comment