Python Google App Engine Receiving a string in stead of JSON object - android

I am sending a HTTP POST request from android to a server using the script below
URI website = new URI("http://venkygcm.appspot.com");
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(website);
request.setHeader("Content-Type", "application/json");
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
JSONObject obj = new JSONObject();
obj.put("reg_id","Registration ID sent to the server");
obj.put("datetime",currentDateTimeString);
StringEntity se = new StringEntity(obj.toString());
request.setEntity(se);
HttpResponse response = client.execute(request);
String out = EntityUtils.toString(response.getEntity());
As I have sent a JSON Object, I must receive a JSON Object in the server. Instead I get a string containing the data of the body. The server is made in Python Google App Engine.
import webapp2
class MainPage(webapp2.RequestHandler):
def post(self):
self.response.out.write(" This is a POST Request \n")
req = self.request
a = req.get('body')
self.response.out.write(type(a))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
I tried what AK09 suggested but i still get a string kind of object. What should be my next step?
import webapp2
import json
class MainPage(webapp2.RequestHandler):
def post(self):
self.response.out.write("This is a POST Request \n")
req = self.request
a = req.get('body')
b = json.dumps(a)
self.response.out.write(type(a))
self.response.out.write(type(b))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

Finally this code worked
import webapp2
import json
class MainPage(webapp2.RequestHandler):
def post(self):
self.response.out.write("This is a POST Request \n")
req = self.request
a = req.body
b = json.loads(a)
self.response.out.write(b)
self.response.out.write(b['reg_id'])
self.response.out.write(b['datetime'])
self.response.out.write(type(b))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
b comes out to be of the type List as is required.

Related

Android FileBody equivalent encoding in iOS

We have some code posting an image and some other data as part of a Multipart in a POST request. In Android is working just fine, but I can't seem to make it work on iOS, where I keep getting a 500 Internal Server Error. The working Android code looks like
String uploadURL = "http://someServer.com/upload";
String imageToUploadPath = "imgFilePath";// path de la imagen a subir
String userId = "123";
String token = "abcd";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uploadURL);
File f = new File(imageToUploadPath);
FileBody fileBody = new FileBody(f);
MultipartEntity reqEntity = new MultipartEntity();
Charset chars = Charset.forName("ISO-8859-1");
reqEntity.addPart("id", new StringBody(userId, chars));
reqEntity.addPart("token", new StringBody(token, chars));
reqEntity.addPart("image", fileBody);
httppost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httppost);
My iOS attempt using AFNetworking is as follows
uploadURLStr = #"http://someServer.com/upload";
NSString *token = #"abcd";
NSString *userID = #"123";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableSet *supportedContentTypes = [manager.responseSerializer.acceptableContentTypes mutableCopy];
supportedContentTypes addObject:#"text/html"];
manager.responseSerializer.acceptableContentTypes = supportedContentTypes;
NSURLSessionTask *task = [manager POST:uploadURLStr
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSDictionary *tokenHeaders = #{#"Content-Disposition": #"form-data",
#"name": #"token",
#"Content-Type": #"text/plain",
#"charset": #"ISO-8859-1",
#"Content-Transfer-Encoding": #"8bit"
};
[formData appendPartWithHeaders:tokenHeaders body:[token dataUsingEncoding:NSISOLatin1StringEncoding]];
NSMutableDictionary *userIDHeaders = [tokenHeaders mutableCopy];
[userIDHeaders setObject:#"id" forKey:#"name"];
[formData appendPartWithHeaders:[userIDHeaders copy] body:[userID dataUsingEncoding:NSISOLatin1StringEncoding]];
NSDictionary *imgHeaders = #{#"Content-Disposition": #"form-data",
#"name": #"image",
#"filename": fileName,
#"Content-Type": #"application/octet-stream",
#"Content-Transfer-Encoding": #"binary"
};
[formData appendPartWithHeaders:imgHeaders
body:[imgData base64EncodedDataWithOptions:0]];
}
progress:^(NSProgress *uploadProgress) {
NSLog(#"progress: %.2f", uploadProgress.fractionCompleted);
}
success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"responseObject = %#", responseObject);
} failure:^(NSURLSessionTask *task, NSError *error) {
[self showUploadError];
NSLog(#"error = %#", error);
}];
Headers for each part we managed to log it from the Android app so we replicated it -for instance, I was sending the image with a Content-Type of image/jpeg and Android sends it as application/octet-stream- I assume that the image data is not being encoded as in Android. I have tried with base64 encoding as it's in the code now -tried different options other than 0-, just leaving the NSData returned by UIImageJPEG representation but I can't hit the nail. Any help is appreciated.

how to use URL to post data with HttpClient in android

I have to send/post some data to .svc Web Service that basically connect to remote database. I'm using JSONStringer to send the data but every time response status is false. My data is not sent. How to use HttpPost in Android . Can someone help me how to solve this .
Here is my webservice code
String namespace = "http://103.24.4.60/xxxxx/MobileService.svc";
public void ActivityUpload( final String strCurrentDateTime, final String strTitle, final String replaceDescChar, final String editedHashTag)
{
new AsyncTask<String, Void, String>()
{
#Override
protected String doInBackground(String... arg0)
{
String line = "";
try
{
Log.e("ActionDate "," = "+ strCurrentDateTime);
Log.e("ActivityId"," = "+strActivityId);
Log.e("UserId"," = "+str_UserId);
Log.e("ObjectId"," = "+strVessId);
Log.e("Name"," = "+strTitle);
Log.e("Remark"," = "+replaceDescChar);
Log.e("Status"," = "+"PENDING");
Log.e("Type"," = "+strType);
Log.e("starflag"," = "+0);
Log.e("HashTag"," = "+editedHashTag);
Log.e("Authentication_Token"," = "+str_Authentication_Token);
// make web service connection
HttpPost request = new HttpPost(namespace + "/Upd_Post_Activity");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer TestApp = new JSONStringer().object()
.key("ActionDate").value(strCurrentDateTime)
.key("ActivityId").value(strActivityId)
.key("UserId").value(str_UserId)
.key("ObjectId").value(strVessId)
.key("Name").value(strTitle)
.key("Remark").value(replaceDescChar)
.key("Status").value("PENDING")
.key("Type").value(strType)
.key("starflag").value("0")
.key("HashTag").value(editedHashTag)
.key("Authentication_Token").value(str_Authentication_Token).endObject();
StringEntity entity = new StringEntity(TestApp.toString());
Log.d("****Parameter Input****", "Testing:" + TestApp);
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving: " + response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
// print status in log
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);
}
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
}.execute();
}
Here is input Parameter.
****Parameter Input****﹕ Testing:{"ActionDate":"2016-01-21%2014:20:43%20PM","ActivityId":"120160119180421058","UserId":"125","ObjectId":"1","Name":"Title2","Remark":"Test%20two","Status":"PENDING","Type":"3","starflag":"0","HashTag":"990075","Authentication_Token":"6321D079-5B28-4F3F-AEE7-D59A1B9EFA59"}
Thanks in advanced.
realize android httpclients are in process of deprecation ( in favor of httpsurlconnection ) but, these httpclients are still used pretty widely. On gradle builds, regard the deprication, and with small dependency lib tweeks , httpclient may be used for some time still.
( still gonna use httpclient ? )
Put android aside for a min.
learn how to CURL with JSON body for tests that show you what you EXACT JSON in body and exact HEADERS you will need to get success http result to a post ... ref here
Once you have that you can then go about transferring your curl test's components over to android.httpclient.exec.POST using httpclient of your choice.
Set the same group of Headers you had over in curl tests in your android post. apache.httpclient sample
2.a. make sure that default list of headers from the clients 'request' constructor does NOT include by default some headers you DO NOT want... In order to assure of this ,you probably will need to turn on HEADER logging for your client.... java example logger . remove unnecessary headers included by the framework constructor of POST.
2.b android logger (WIRE, HEADERS) is diff from and may take some digging , depend on what client is in use.
with the same headers as curl tests, set the http.posts request.entity to either a string or a properly encoded array of bytes containing the same JSON body used in the curl tests.
3.A. depending on the JSON lib, create your message objects and then convert the objects to some friendly type for enclosure in an entity for the post ie use a 'writer' to convert objects to a serialized string with the JSON.
reqRole = new ObjectMapper().createObjectNode();
reqRole.put("__type", "Pointer");
reqRole.put("className", "_Role");
reqRole.put("objectId", roleId);
rootOb.put("requestedRole", reqRole);
rootOb.put("requestedBy",usersArr);
StringWriter writer = new StringWriter();
try {
new ObjectMapper().writeValue(writer, rootOb)
..
String http-post-str=writer.toString();
3.B. wrap the string with json in the POST request...
httpPost.setEntity(new StringEntityHC4(http-post-str));
exec the request and youll get the same results you got in curl because the headers are same or nearly same and the body is the same , encoded string of json. same input = same result

Trouble sending JSON Post Android

It has been a while since I programmed for Android and I have lost all my previous work which had the code in it I am having problems with. I am developing an app for both Android and iPhone which connect to the same server to download data. All is well in the iPhone version but on Android when I hit the server with the post data containing the method name I would like to to run on the server it seems that the data is not added to the request.
Why is the POST not working in this request for Android but does for the iPhone version of the app?
Here is the code I am using:
public static void makeRequest() throws Exception {
Thread t = new Thread() {
public void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpEntity entity;
JSONObject json = new JSONObject();
try {
HttpPost post = new HttpPost("http://divisi.co.uk/rest/requesthandler.php");
json.put("method", "getEventListData");
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
entity = response.getEntity();
String retSrc = EntityUtils.toString(entity);
JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object
if(result.getString("SC") == "200"){
JSONArray data = result.getJSONArray("data");
}
else{
}
} catch(Exception e) {
e.printStackTrace();
}
Looper.loop(); //Loop in the message queue
}
};
t.start();
}
The response I get mack from the server is:
{"data":{"scalar":""},"SC":405,"timestamp":1363788265}
Meaning the method name was not found, i.e. not posted in my request to the server.
heres an example of how i do things like this:
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://divisi.co.uk/rest/requesthandler.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart(new FormBodyPart("method", new StringBody("getEventListData")));
reqEntity.addPart(new FormBodyPart("NEED_A_KEY_HERE", new StringBody("" + json.toString())));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
JSONObject responseDict = new JSONObject(EntityUtils.toString(response.getEntity()));
allow this is your "http://divisi.co.uk/rest/requesthandler.php" page code, then in android you can use this... you don't allow post in your URL,
use fiddler on your sever side. see if the http message is correct. it seems your sever side problem, can you show us your sever side code which receive and parse json.
If the server can't read your request try to remove:
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
It will use the mime type defaults HTTP.PLAIN_TEXT_TYPE i.e. "text/plain".
I don't see any other possibility, if your code is the one you posted and not a more complicated input JSON object.
Your code to set the POST body may be just fine. I think the problem may be with your web service. Try using something like Rested or curl to manually make the call to your server. I made exactly the same request you are making, including with and without the POST body, and I got the same response from your server:
{"data":{"scalar":""},"SC":405,"timestamp":1365704082}
Some things that may be tripping you up:
JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object
if(result.getString("SC") == "200"){
JSONArray data = result.getJSONArray("data");
}
Here, you are comparing the string "405" to "200" using ==, when you should first do a null check and then use .equals("200") instead. Or, use result.getInt("SC") == 200 since this is an integer type in your response JSON.
Also, the "data" entity from your server response is not actually coming back as a JSON array. You should use getJSONObject("data") instead.
Additionally, it's always a good idea to externalize your strings.
Here's how the code should look:
public static final String JSON_KEY_SC = "SC";
public static final String JSON_KEY_DATA = "data";
...
JSONObject result = new JSONObject(retSrc); //Convert String to JSON Object
String sc = result.getString(JSON_KEY_SC);
if (sc != null && sc.equals("200")) {
JSONObject data = result.getJSONObject(JSON_KEY_DATA);
}
else {
...
}

How receive JSON object via HttpClient?

I use HttpClient in android to send post request:
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(hostNameCollection);
StringEntity se = new StringEntity(jsonObj.toString());
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setEntity(se);
HttpResponse response = client.execute(request);
Log.v("HttpLogClient.logJSONObject", "wyslano JSON");
and I'dont know how I can receive JSON object on my Java EE servlet.
you need to read the response body text, then parse as JSON,
String result = EntityUtils.toString(response.getEntity());
JSONObject jo = new JSONObject(result);
read the body of the http post ( server-side ) by getting the a stream object on the body and then reading it.
Once youve read it , convert the bytes to chars and that will be json which you can use to build a json object like a jsonNode using 'jackson' libs.
If you are using plain servlets the json stream is located in the body of the HttpServletRequest : request.getReader() or request.getInputStream();
To make things easier you could use a library handling databinding for you.
Have a look at Genson http://code.google.com/p/genson/.
YouClass object = new Genson().deserialize(request.getReader(), YourClass.class);
// or to a plain map
Map<String, Object> map = genson.deserialize(request.getReader(), Map.class);

Sessions and file upload between Drupal and Android

I'm trying to upload an image file from an android device to my drupal website using services module
I can log-in succesfully:
HttpParams connectionParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(connectionParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(connectionParameters, timeoutSocket);
httpClient = new DefaultHttpClient(connectionParameters);
HttpPost httpPost = new HttpPost(serverUrl+"user/login");
JSONObject json = new JSONObject();
try{
json.put("password", editText_Password.getText().toString());
json.put("username", editText_UserName.getText().toString());
StringEntity se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
//Execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
int status_code = response.getStatusLine().getStatusCode();
...
...
}
catch(Exception ex)
{
}
via response object I can get session name session id , user id and many other info.
after the login , I set no session info by myself through my HttpGet object , but use the same DefaultHttpClient, I can magically retrieve a node using the following code:
HttpGet httpPost2 = new HttpGet(serverUrl+"node/537.json");
HttpResponse response2 = httpClient.execute(httpPost2);
this made me think that, httpClient object stored the session info for me automatically.
because if I dont login first or use a new HttpClient object and try to retrieve the node, I get a 401 error.
However when I try to upload an image file as follows after logging in:
httpPost = new HttpPost(serverUrl+"file/");
json = new JSONObject();
JSONObject fileObject = new JSONObject();
fileObject.put("file", photodata); //photodata is a byte[] that is set before this point
fileObject.put("filename", "myfirstfile");
fileObject.put("filepath", "sites/default/files/myfirstimage.jpg");
json.put("file", fileObject);
se = new StringEntity(json.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
//Execute HTTP post request
response = httpClient.execute(httpPost);
status_code = response.getStatusLine().getStatusCode();
I get 401 error although I'm logged in and using the same HttpClient object.
I also tried adding :
httpPost.setHeader("Cookie", SessionName+"="+sessionID);
which again gives me 401 error.
I'm also not sure whether I'm using the correct url, because I'm trying to use file.create method, but writing the url as "myip:myport/rest/file/create" gives wrong address.
My aim is to upload an image to a users node, so I guess after succesfully adding the file, I'll use node.create right?
I hope someone will help me get through this.
I found when I first started doing this that most of my errors were due to not authenticating correctly.. I'm not sure your method is correct.. I know this works.
Using Drupal Services 3, I login as such and then store my session cookie into shared preferences. dataOut is a JSON object which holds the needed user login, and password information.
String uri = URL + ENDPOINT + "user/login";
HttpPost httppost = new HttpPost(uri);
httppost.setHeader("Content-type", "application/json");
StringEntity se;
try {
se = new StringEntity(dataOut.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httppost.setEntity(se);
HttpResponse response = mHttpClient.execute(httppost);
mResponse = EntityUtils.toString(response.getEntity());
// save the sessid and session_name
JSONObject obj = new JSONObject(mResponse);
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(mCtx);
SharedPreferences.Editor editor = settings.edit();
editor.putString("cookie", obj.getString("session_name") + "="
+ obj.getString("sessid"));
editor.putLong("sessionid_timestamp", new Date().getTime() / 100);
editor.commit();
} catch { //all of my catches here }
Once I have my session id stored.. I go about performing tasks on drupal like this.. The following code posts a node. I use the function getCookie() to grab the session cookie if it exists.. if not, then I log in, or if it's expired, I log in. (note, you need to set the cookie expire time in your drupal settings.php file (I think that's where it is if I remember correctly)
String uri = URL + ENDPOINT + "node";
HttpPost httppost = new HttpPost(uri);
httppost.setHeader("Content-type", "application/json");
String cookie = this.getCookie(mCtx);
httppost.setHeader("Cookie", cookie);
StringEntity se;
try {
se = new StringEntity(dataOut.toString());
httppost.setEntity(se);
HttpResponse response = mHttpClient.execute(httppost);
// response is here if you need it.
// mResponse = EntityUtils.toString(response.getEntity());
} catch { //catches }
The getCookie() function that keeps your cookie uptodate and working..
/**
* Takes the current time, the sessid and determines if we are still part of
* an active session on the drupal server.
*
* #return boolean
* #throws InternetNotAvailableException
* #throws ServiceNotAvailableException
*/
protected String getCookie(Context ctx)
throws InternetNotAvailableException {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(mCtx);
Long timestamp = settings.getLong("sessionid_timestamp", 0);
Long currenttime = new Date().getTime() / 100;
String cookie = settings.getString("cookie", null);
//mSESSION_LIFETIME is the session lifetime set on my drupal server
if (cookie == null || (currenttime - timestamp) >= mSESSION_LIFETIME) {
// the following are the classes I use to login.
// the important code is listed above.
// mUserAccount is the JSON object holding login,
// password etc.
JSONObject mUserAccount = UserAccount.getJSONUserAccount(ctx);
call(mUserAccount, JSONServerClient.USER_LOGIN);
return getCookie(ctx);
} else {
return cookie;
}
}
This really should enable you to take advantage of all that Services has to offer. Make sure your endpoints are correct, and also make sure your permissions are set. I cursed for hours before I realized I had not granted perms to make nodes to users.
So once you are logged in.. To upload a file to Drupal Services I use the following code to first convert the image to byteArray.. and then to Base64.
tring filePath = Environment.getExternalStorageDirectory()+ "/test.jpg";
imageView.setImageDrawable(Drawable.createFromPath(filePath));
Bitmap bm = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
Once you have the encodedImage. Construct a JSON Object with the keys, file (required), filename (optional, but recommended), filesize (optional) and uid (optional, the poster I presume) JSON would therefore look like this at its simplist required form {"file":encodedImage}. Then, after making sure you have enabled the file resource on your server, POST the data to my-server/rest-endpoint/file. The response will include a fid in JSON. You can then assign this fid to the image field of a node you subsequently create using the node resource.

Categories

Resources