I was trying to connect the android application to the WCF service but it's not working. WCF is hosted on the IIS server. I don't know which one is wrong android application or WCF Service itself. WCF service is working fine when tested. Here is my WCF service code.
[ServiceContract(Namespace = "http://services.example.com")]
public interface IEmployeeInfo
{
[OperationContract]
[WebInvoke(
BodyStyle = WebMessageBodyStyle.Wrapped,
Method="Get",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetEmployee/?key={employeeId}" )]
Employee GetEmployee(int employeeId);
}
Here is my android code in which I am accessing the WCF service
try {
DefaultHttpClient client = new DefaultHttpClient();
// http get request
HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI + evEmployeeId.getText());
Log.d("Connect","Connecting to Server 0");
Log.d("Connect","Connecting to Server 1");
// set the hedear to get the data in JSON formate
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
//get the response
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
//if entity contect lenght 0, means no employee exist in the system with these code
if(entity.getContentLength() != 0) {
// stream reader object
Reader employeeReader = new InputStreamReader(response.getEntity().getContent());
//create a buffer to fill if from reader
char[] buffer = new char[(int) response.getEntity().getContentLength()];
//fill the buffer by the help of reader
employeeReader.read(buffer);
//close the reader streams
employeeReader.close();
Log.d("Connect","Connecting to Server 2");
//for the employee json object
JSONObject employee = new JSONObject(new String(buffer));
//set the text of text view
tvEmployeeCode.setText("Code: " + employee.getString("EmployeeId"));
tvName.setText("Name: " + employee.getString("FirstName") + " " + employee.getString("LastName"));
tvAddress.setText("Address: " + employee.getString("Address"));
tvBloodGroup.setText("Blood Group: " + employee.getString("BloodGroup"));
}
else {
text.setTextSize(R.string.text);
}
}
catch (Exception e) {
Log.d("Error",e.getMessage());
e.printStackTrace();
}
Any help is appreciated.
When I request the WCF service from android emulator applcation crashes. here the logchat.
11-17 22:56:55.566: W/dalvikvm(1174): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-17 22:56:55.616: E/AndroidRuntime(1174): FATAL EXCEPTION: main
11-17 22:56:55.616: E/AndroidRuntime(1174): java.lang.NullPointerException: println needs a message
11-17 22:56:55.616: E/AndroidRuntime(1174): at android.util.Log.println_native(Native Method)
11-17 22:56:55.616: E/AndroidRuntime(1174): at android.util.Log.d(Log.java:138)
11-17 22:56:55.616: E/AndroidRuntime(1174): at com.yyousuf.sample.EmployeeInfoActivity.onClick
There's an exception being thrown, but because you are catching all Exceptions, you don't have the details. For some reason the throwed Exception doesn't have a message set, and that's why you are getting the NullPointerException on this line
Log.d("Error",e.getMessage());
Please post more information about the real throwed exception.
Related
I am having trouble posting a JSONArray of values to my WCF Service. When I post the data from Fiddler or .Net Test Client it works fine. Every time I try to post from my android application I get Request Error
This is the JSON data that I send to my WCF Service from the android application. I've tried this exact data from Fiddler and it works
[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"},
"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"},
"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}]
My android code
public JSONObject makeHttpPost(String url, String method, JSONArray params) {
try {
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type",
"application/json; charset=utf-8");
StringEntity se = new StringEntity(params.toString(),"UTF-8");
se.setContentType("application/json;charset=UTF-8");
httpPost.setEntity(se);
Log.e("Gerhard", params.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
My WCF Service
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "updateOrderAddress")]
String UpdateOrderAddress(Stream JSONdataStream);
public String UpdateOrderAddress(Stream JSONdataStream)
{
try
{
// Read in our Stream into a string...
StreamReader reader = new StreamReader(JSONdataStream);
string JSONdata = reader.ReadToEnd();
// ..then convert the string into a single "wsOrder" record.
if (JSONdata == null)
{
// Error: Couldn't deserialize our JSON string into a "wsOrder" object.
return "null";
}
return JSONdata; // Success !
}
catch (Exception e)
{
return e.ToString();
}
}
The error I'm getting
02-26 14:00:56.185: E/Gerhard(31064): <p>The server encountered an error processing the request. The exception message is 'Incoming message for operation 'UpdateOrderAddress' (contract 'IService1' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>
I have called multiple GET requests from android application to same WCF Service and it works great, but now I need to send an array of data to the wcf service. Please please help me.Thanks in advance
remove
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
from ur code
I'm working on a Android application and I'm trying to get a JSON response from a server which is configured to return a json object (".../current_user.json") when receives a GET message, but the answer I get is in HTML format and not in JSON format as expected.
I don't understand why is this happening because I did the same requests on the browser and with the program RESTClient and got the right answer in JSON format.
Here is the code I'm using.
JSONObject json = new JSONObject();
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 10000);
HttpClient httpClient = new DefaultHttpClient(params);
HttpGet get = new HttpGet(url_getiduser);
HttpResponse response = httpClient.execute(get);
String sresponse = "error";
Log.d("url get", url_getiduser);
Log.d("pedido get", get.getMethod());
if(response != null)
{
InputStream in = response.getEntity().getContent();
sresponse = convertStreamToString(in);
Log.d("resposta http", sresponse);
if(!sresponse.equals("error"))
{
JSONObject object = new JSONObject(sresponse);
id_user = (String) object.get("id");
json = object;
Log.d("objecto json", object.toString());
}
else Log.d("Error on json parser", sresponse);
There are few cases where you get HTML text
You might have called a wrong function which gives a 404 page.
Might be a database error on server side where you will get database error message
Server might be sending a styled data which has HTML tags
But you better Log the response and paste it here.
I'm trying to logoff from the server. But it returns "0" response code with this exception. I'm using GET verb to do this.
LogCat
10-17 14:54:13.261: W/System.err(868): java.io.IOException: Received authentication challenge is null
10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processAuthHeader(HttpURLConnectionImpl.java:397)
10-17 14:54:13.284: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.processResponseHeaders(HttpURLConnectionImpl.java:345)
10-17 14:54:13.304: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:276)
10-17 14:54:13.324: W/System.err(868): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
10-17 14:54:13.324: W/System.err(868): at com.remote.synchronizer.haris.CustomHttpClient.executeGet(CustomHttpClient.java:131)
10-17 14:54:13.354: W/System.err(868): at com.remote.synchronizer.haris.OptionsActivity$1$3$1.run(OptionsActivity.java:87)
10-17 14:54:13.364: W/System.err(868): at android.os.Handler.handleCallback(Handler.java:605)
10-17 14:54:13.384: W/System.err(868): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 14:54:13.384: W/System.err(868): at android.os.Looper.loop(Looper.java:137)
10-17 14:54:13.404: W/System.err(868): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-17 14:54:13.424: W/System.err(868): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 14:54:13.424: W/System.err(868): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 14:54:13.454: W/System.err(868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 14:54:13.474: W/System.err(868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 14:54:13.474: W/System.err(868): at dalvik.system.NativeStart.main(Native Method)
10-17 14:54:13.484: E/HTTP Response(868): java.io.IOException: Received authentication challenge is null
CustomHttpClient.java
public class CustomHttpClient {
static HttpClient client = new DefaultHttpClient();
static HttpURLConnection connection = null;
public static int executePost(String url, String postParameters)
{
int response=0;
OutputStream output = null;
try
{
connection = (HttpURLConnection)new URL(url).openConnection();
System.setProperty("http.keepAlive", "false");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
connection.connect();
output = connection.getOutputStream();
output.write(postParameters.getBytes("UTF-8"));
response=connection.getResponseCode();
}
catch(Exception e)
{
e.printStackTrace();
Log.e("HTTP Response", e.toString());
}
finally {
if(connection != null) {
// connection.disconnect();
if (output != null)
try { output.close(); }
catch (IOException logOrIgnore) {}
}
}
return response;
}
public static int executeGet(String url)
{
int response=0;
//HttpURLConnection connection = null;
try
{
connection = (HttpURLConnection) new URL(url).openConnection();
System.setProperty("http.keepAlive", "false");
//connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
response=connection.getResponseCode();
}
catch(Exception e)
{
e.printStackTrace();
Log.e("HTTP Response", e.toString());
}
finally {
if(connection != null) {
// connection.disconnect();
}
}
return response;
}
}
Before this I'm using DefaultHTTPClient in Gingerbird 2.3 its working perfectly but in ICS DefaultHTTPClient is not working so I need to use HttpURLConnection. POST verb is working fine.
You can get the response code after an exception if you call .getResponseCode() a second time on the connection object. This is because the first time you call .getResponseCode() an internal state is set that enables .getResponseCode() to return without throwing an exception.
Example:
HttpURLConnection connection = ...;
try {
// Will throw IOException if server responds with 401.
connection.getResponseCode();
} catch (IOException e) {
// Will return 401, because now connection has the correct internal state.
int responsecode = connection.getResponseCode();
}
I have also answered this question here: https://stackoverflow.com/a/15972969/816017
HttpURLConnection.getResponseCode() throws java.io.IOException: Received authentication challenge is null when it encounters malformed HTTP 401 header. Do you receive WWW-Authenticate and Content-Length headers from server in addition to HTTP/1.1 401 Unauthorized header? See IOException: "Received authentication challenge is null" (Apache Harmony/Android)
If you can't make changes to server, then you can catch that exception (thanks https://stackoverflow.com/a/10904318/262462)
try {
response=connection.getResponseCode();
}
catch (java.io.IOException e) {
if (e.getMessage().contains("authentication challenge")) {
response = HttpsURLConnection.HTTP_UNAUTHORIZED;
} else { throw e; }
}
This error happens beause the server sends a 401 (Unauthorized) but does not give a "WWW-Authenticate" which is a hint for the client what to do next. The "WWW-Authenticate" Header tells the client which kind of authentication is needed (either Basic or Digest). This is usually not very useful in headless http clients, but thats how the standard is defined. The error occurs because the lib tries to parse the "WWW-Authenticate" header but can't.
Possible solutions if you can change the server:
Add a fake "WWW-Authenticate" header like: WWW-Authenticate: Basic realm="fake". This is a mere workaround not a solution, but it should work and the http client is satisfied.
Use HTTP status code 403 instead of 401. It's semantic is not the same and usually when working with login 401 is a correct response (see here for detailed discussion) but its close enough.
Possible solutions if you can't change the server:
As #ErikZ wrote in his post you could use a try&catch
HttpURLConnection connection = ...;
try {
// Will throw IOException if server responds with 401.
connection.getResponseCode();
} catch (IOException e) {
// Will return 401, because now connection has the correct internal state.
int responsecode = connection.getResponseCode();
}
I also posted this here: java.io.IOException : No authentication challenges found
I recently worked on an application which connects and parse data from a webserver into an android device, tested on froyo and gingerbread and works fine but crashes on ICS devices. As for the UI I used gingebread objects so I think it's not the issue.
It works just fine on the first run even after connecting to facebook but errors on the part where it fetch and parse all of the JSON data gathered.
Here's the logcat errors straight from the device:
E/AndroidRuntime(25620): FATAL EXCEPTION: Thread-9660
E/AndroidRuntime(25620): java.lang.UnsupportedOperationException
E/AndroidRuntime(25620): at java.lang.Thread.stop(Thread.java:1076)
E/AndroidRuntime(25620): at java.lang.Thread.stop(Thread.java:1063)
E/AndroidRuntime(25620): at com.android.guestlist.SplashScreen$1.run(Spla
shScreen.java:89)
E/android.os.Debug( 2090): !#Dumpstate > dumpstate -k -t -n -z -d -o /data/log/d
umpstate_app_error
E/Launcher(18546): Error finding setting, default accessibility to not found: ac
cessibility_enabled
E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion
E/log_tag (25620): Error converting result java.lang.NullPointerException
E/log_tag (25620): Error parsing data org.json.JSONException: End of input at ch
aracter 0 of
E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion
E/log_tag (25620): Error converting result java.lang.NullPointerException
E/log_tag (25620): Error in http connection android.os.NetworkOnMainThreadExcept
ion
E/log_tag (25620): Error parsing data org.json.JSONException: End of input at ch
aracter 0 of
And here's my JSON parser since I think this is where the problem happens? And oh, the logs I created doesn't even appear of the phone logs Well I can't really say but here is the code:
public void getDataFromWeb(String a, String b){
String result = "";
Log.v(TAG, "Name Value Pair a " + a);
Log.v(TAG, "Name Value Pair b " + b);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("a_param",a));
nameValuePairs.add(new BasicNameValuePair("b_param",b));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://[mywebserviceshere].php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.v(TAG, "connected");
}catch(Exception e){
Log.v(TAG, "run failed");
Log.e("log_tag", "Error in http connection "+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.v(TAG, "buffered read");
}catch(Exception e){
Log.v(TAG, "buffered error");
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
Log.v(TAG, result);
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
Log.v(TAG, "loop start");
json_data = jArray.getJSONObject(i);
w.add(json_data.getString("w_data"));
x.add(json_data.getString("x_data"));
y.add(json_data.getString("y_data"));
z.add(json_data.getString("y_data"));
Log.v(TAG, "list added");
}
}catch(JSONException e){
w.add("0");
x.add("No Data");
y.add("No Data");
z.add("No Data");
Log.v(TAG, "rest failed");
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
And finally here's exactly what it looks like in AVD:
Is this an issue on ICS or I have to revise some of my codes?
You've got a NetworkOnMainThreadException and that is generally a sign of poor code, and it'll result in a bad user experience because the application locks up whenever it's running some sort of network activity. Yes, it (unfortunately) works on pre-Honeycomb devices, but it's not what anyone should be going for.
To solve your problem, I'd highly recommend you use a single AsyncTask and do all your HTTP calls in the doInBackground() method. Once they're completed, the onPostExecute() method will be called automatically, so you can update the GUI. Quite simple.
Take a look at the documentation for AsyncTask and understand it before doing anything else. I'm sure this will work perfectly for your application: http://developer.android.com/reference/android/os/AsyncTask.html
So no - it's not a problem with Ice Cream Sandwich. It simply won't allow you to make network requests on the main thread because it can block everything else.
(this is pretty similar to another question I answered a few days ago, so part of the answer is copied from there: https://stackoverflow.com/a/11897381/762442)
You are getting this exception because you are performing network operation in main thread
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://[mywebserviceshere].php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
This was allowed before Honeycomb but after honeycomb this has been stopped and if you do so, you will get NetworkOnMainThreadException.
So its better to use AsyncTask. Using this, you can perform network operation in background thread(other than main thread).
To know more how AsyncTask works, see this link
Use the Asynctask in your code your getting NetworkOnMainThreadException ,
not only with ics but also with honeycomb it gives you the same Exception.
I need to provide an RESTful api to be developed in ruby on rails with CRUD features for an android app. I haven't worked with mobile phone OS(android/iphone) integration with RoR earlier. So, please help me getting started.
Thanks in advance.
you can send the data as Json packets to the device and in device parse the json packets and access the data. your calls to webservice should be a http call eg this is for the client.
http:\server\metnod\get_somedata?name=something
and the server should query the database for this parameter and send you the reponse as Json. parse json and get your details.
String url = "http:\\example.com\mycontroller\get_employee?id=2"
HttpPost httpPostRequest = new HttpPost(url);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Authorization", usercredential);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
Log.v(null, "resultString "+resultString);
instream.close();
// Transform the String into a JSONObject
if(resultString!=null){
jsonObjRecv = new JSONObject(resultString);
}
// Raw DEBUG output of our received JSON object:
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
In the server side you should have a get_employee method in a controller called mycontroller. in the method you can process the request as a normal http request and send the response as json eg
employee = Employee.find_by_id(params[:id])
#js_response = ActiveSupport::JSON.encode(employee)
respond_to do |format|
format.json { render :json => #js_response}
format.html
end
for CRUD you need to create different methods with appropriate parameters like delete_employee, update_employee etc. refer json.org to create/parse json in android