Call dotnet asmx webservice with parameter using android volley library in android - android

I am facing an issue calling .net .asmx web service with parameter using android volley library.
Without parameter its working fine.
This my web service
[WebMethod]
private string MN_InsEOMTestScoreDetailsIndividual(string data)
{
ArrayList arrReturnDetails = new ArrayList();
bool bReturn = false;
string errMsg = globalErrMsg;
try
{
Dictionary<string, object> dicData = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);
bReturn = InsEOMTestScoreDetailsIndividual(dicData["eomTSIndvlDetId"].ToString().Trim(), dicData["eomTRDetId"].ToString().Trim(), dicData["eomTSSummDetId"].ToString().Trim(),
dicData["studCode"].ToString().Trim(), dicData["ctrCode"].ToString().Trim(), dicData["batCode"].ToString().Trim(), dicData["phCode"].ToString().Trim(), dicData["pcCode"].ToString().Trim(),
dicData["tmCode"].ToString().Trim(), dicData["testActualDur"].ToString().Trim(),
dicData["testAttemptDur"].ToString().Trim(), dicData["testTtlMks"].ToString().Trim(), dicData["mksObt"].ToString().Trim(), dicData["ttlQues"].ToString().Trim(),
dicData["rAnsCnt"].ToString().Trim(), dicData["wAnsCnt"].ToString().Trim(), dicData["attemptTestDate"].ToString().Trim(),
dicData["isSolViewed"].ToString().Trim(), dicData["quesXML"].ToString().Trim(), out errMsg);
}
catch (Exception ex)
{
ErrorHandler.LogError("WebService:APP_UserActivityDetails", "M_InsEOMTestScoreDetailsIndividual", ex);
}
arrReturnDetails.Add(new
{
b = bReturn,
err = errMsg
});
return JsonConvert.SerializeObject(arrReturnDetails);
}

Your web service is probably a GET web service which you are trying to invoke as POST. That won't work. There are two ways to correct this:
Make sure your web service is of POST type and then add your parameters as a HashMap in the Volley request.
If you want to keep the web service as a GET, then append the parameters manually to the URL string (i.e. don't pass them as key-value pairs in a HashMap).

Related

passing JSON object to a web service through AsyncTask

I am trying to pass a JSON object to a web service through Asynctask. I call a collectStatistics method in the MainActivity. This method collects some statistics of the mobile device and combines them together in a JSON object. JSON object is then passed to Asynctask.
JSONObject jsonProperties = new JSONObject();
try {
jsonProperties.put("_device" , _device);
jsonProperties.put("_model", _model);
jsonProperties.put("_product", _product);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonProperties.length() > 0)
{
//call to async class
//String.valueOf(jsonProperties)
//new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties); //instead of execute
Toast.makeText(context,jsonProperties.toString(),Toast.LENGTH_LONG).show();
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
new SendJsonProperties().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, jsonProperties);
Log.i("msg","calling asynctask");
} else {
new SendJsonProperties().execute(jsonProperties);
Log.i("msg","NOT calling asynctask");
}
}
The Asynctask receives JSON parameter
class SendJsonProperties extends AsyncTask <JSONObject,String,String>
I call a web service caller method in doInBackground
protected String doInBackground(JSONObject... params)
{
//(...) elippsis make the input array
Log.i("msg", "do in bkgnd");
WebServicesCaller webServicesCaller = new WebServicesCaller();
result = webServicesCaller.storeStatistics(params[0]);
return null;
}
But I get this error
java.lang.RuntimeException: Cannot serialize: {"_product":"ms013gxx","_model":"SM-G7102","_device":"ms013g"}
I cannot figure out the problem in serialization. Any help will be much appreciated. Thanks in advance.
You have to make Model POJO class of your JSONObject parameters.
Use this link to create POJO class according to JSON REsponse.
http://www.jsonschema2pojo.org/
I was trying to send a JSON object to a web method which takes a string parameter
public string storeStatistics(string jsonObj)
So I changed
jsonProp.setType(JSONObject.class);
request.addProperty(jsonProp, obj);
to
jsonProp.type = PropertyInfo.STRING_CLASS;
request.addProperty(jsonProp, obj.toString());

How to receive file on Spring server send from a client using retrofit2

I am currently trying to upload a file from an android client using retrofit2 to a server using Spring Boot and its REST api.
CLIENT
I specifiy the upload method as described here: https://github.com/square/retrofit/issues/1063
public interface RetroRespondService {
#Multipart
#POST("/v1/answers")
public Call<ResponseDTO> sendPictures(#Part("file\"; filename=\"image.png")RequestBody image);
}
In another class the method to provide the actual image is declared:
(Now its just a test scenario. When image uploading is actually accomplished it will get more sophisticated.)
public void performAnswerRequest() {
try {
if (mRetrofit == null) {
mRetrofit = new Retrofit.Builder()
.baseUrl(DataHolder.getHostName())
.build();
}
//load test image
AssetManager manager = getAssets();
File file = new File(getFilesDir(), "image.png");
Utility.writeBytesToFile(new BufferedInputStream(manager.open("heart.png")), file);
RetroRespondService requestService = mRetrofit.create(RetroRespondService.class);
RequestBody image= RequestBody.create(MediaType.parse("multipart/form-data"), file);
Call<ResponseDTO> response = requestService.sendPictures(image);
response.enqueue(new AsyncAnswerResponse());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
SERVER
What I actually do not know is, how to properly get the image on the spring side.
#RequestMapping(value = API_VERSION + "/answers", method = RequestMethod.POST)
#ResponseBody
ResponseEntity<ResponseDTO> addAnswers(#RequestParam("file\"; filename=\"image.png") MultipartFile answers) throws DBEntryDoesNotExistException, EvaluationException, ParticipantException {
// In fact I have set a brake point here. Never entered the method yet, though
System.out.println("Yay!")
return null;
}
ERROR
Request: localhost:8080/v1/answers raised org.springframework.web.bind.MissingServletRequestParameterException:
Required MultipartFile parameter 'file"; filename="image.png' is not present
Since wireshark reports that in fact a request of size 1894 Bytes was send and this is the size of the image i want to upload I strongly believe the the data is actually transmitted but cannot be decoded from the server.
I have also seen this answers: How to config "CommonsMultipartResolver" in spring4 without xml to upload file
and subsequently implemented this class on the server side:
#Configuration
public class MultipartConfiguration {
#Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(1048576);
return resolver;
}
}
If you have any pointers in how to solve this I would appreciate your answer tremendously :)
If there are any questions left unanswered feel free to ask away.
Btw.: Sending and receiving JSON encoded data works just fine in both directions.

How do I use RunOnUiThread to update some TextViews on a screen

Alrighty, so I understand that this general question has been asked numerous times here, but I have yet to find an answer that makes sense to me. Almost every answer I've seen just says some blurb like, "hey, just throw this in your method and you're good", but I'm not seeing full examples, and what I've tried is not working either.
Here's the error I receive:
[mono] android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
So, simply put, I have an activity that grabs some information from a web service and then throws the web service results into a couple of TextViews. Could someone please help me figure out where and how I need to use the RunOnUiThread()? Here's the code:
using Android.App;
using Android.OS;
using System;
using System.Web;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Android.Widget;
namespace DispatchIntranet
{
[Activity (Label = "#string/Summary")]
public class SummaryActivity : Activity
{
private static readonly Log LOG = new Log(typeof(SummaryActivity));
private TextView summaryTotalRegularLabel;
private TextView summaryTotalRollover;
private TextView summaryScheduledLabel;
private TextView summaryRemainingRegular;
private string url;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// SET THE LAYOUT TO BE THE SUMMARY LAYOUT
SetContentView(Resource.Layout.Summary);
// INITIALIZE CLASS MEMBERS
init();
if (LOG.isInfoEnabled())
{
LOG.info("Making call to rest endpoint . . .");
if (LOG.isDebugEnabled())
{
LOG.debug("url: " + this.url);
}
}
try
{
// BUILD REQUEST FROM URL
HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(this.url));
// SET METHOD TO 'GET'
httpReq.Method = GetString(Resource.String.web_service_method_get);
// ASK FOR JSON RESPONSE
httpReq.Accept = GetString(Resource.String.web_service_method_accept);
// INVOKE ASYNCHRONOUS WEB SERVICE
httpReq.BeginGetResponse((ar) => {
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse (ar))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
// PUT RESPONSE INTO STRING
string content = reader.ReadToEnd();
// CONVERT STRING TO DYNAMIC JSON OBJECT
var json = JsonConvert.DeserializeObject<dynamic>(content);
if (LOG.isDebugEnabled())
{
LOG.debug("content: " + content);
LOG.debug("json: " + json);
LOG.debug("TOTAL_REGULAR_PTO_HOURS: " + json.d[0].TOTAL_REGULAR_PTO_HOURS);
}
// ** THIS IS WHAT WILL NOT WORK **
this.summaryTotalRegularLabel.Text = json.d[0].TOTAL_REGULAR_PTO_HOURS;
this.summaryTotalRollover.Text = json.d[0].TOTAL_ROLLOVER_PTO_HOURS;
this.summaryScheduledLabel.Text = json.d[0].TOTAL_USED_PTO_HOURS;
this.summaryRemainingRegular.Text = json.d[0].TOTAL_REMAINING_PTO_HOURS;
}
}
}, httpReq);
}
catch (Exception e)
{
LOG.error("An exception occurred while attempting to call REST web service!", e);
}
}
private void init()
{
// GET GUID FROM PREVIOUS INTENT AND DETERMINE CURRENT YEAR
string guid = Intent.GetStringExtra("guid");
int year = DateTime.Now.Year;
// BUILD URL
this.url = GetString(Resource.String.web_service_url)
+ GetString(Resource.String.ws_get_pto_summary)
+ "?" + "guid='" + HttpUtility.UrlEncode(guid) + "'"
+ "&" + "year=" + HttpUtility.UrlEncode(year.ToString());
// GET THE SUMMARY LABELS
this.summaryTotalRegularLabel = FindViewById<TextView>(Resource.Id.SummaryTotalRegular);
this.summaryTotalRollover = FindViewById<TextView>(Resource.Id.summaryTotalRollover);
this.summaryScheduledLabel = FindViewById<TextView>(Resource.Id.summaryScheduledLabel);
this.summaryRemainingRegular = FindViewById<TextView>(Resource.Id.SummaryRemainingRegular);
}
}
}
When you make a web service call, HttpWebRequest creates a new thread to run the operation on. This is done to keep your user interface from locking up or skip frames. Once your web service call is complete, you need to go back to the UI Thread to update the UI components that live on that thread. You can do that a couple of different ways.
First, you can wrap your code in an anonymous function call like so:
RunOnUiThread(()=>{
this.summaryTotalRegularLabel.Text = json.d[0].TOTAL_REGULAR_PTO_HOURS;
this.summaryTotalRollover.Text = json.d[0].TOTAL_ROLLOVER_PTO_HOURS;
this.summaryScheduledLabel.Text = json.d[0].TOTAL_USED_PTO_HOURS;
this.summaryRemainingRegular.Text = json.d[0].TOTAL_REMAINING_PTO_HOURS;
});
Or you can call a function via RunOnUiThread (jsonPayload is a field on the class):
jsonPayload = json;
RunOnUiThread(UpdateTextViews);
...
void UpdateTextViews()
{
this.summaryTotalRegularLabel.Text = jsonPayload.d[0].TOTAL_REGULAR_PTO_HOURS;
this.summaryTotalRollover.Text = jsonPayload.d[0].TOTAL_ROLLOVER_PTO_HOURS;
this.summaryScheduledLabel.Text = jsonPayload.d[0].TOTAL_USED_PTO_HOURS;
this.summaryRemainingRegular.Text = jsonPayload.d[0].TOTAL_REMAINING_PTO_HOURS;
}

android-json-rpc, receiving invalid response

I am trying to get responses from a JSON-RPC Service on Android, I'm currently developing on 3.0 Honeycomb.
This is the library I am using:
http://code.google.com/p/android-json-rpc/
and I am using this JSON-RPC service page for testing:
http://www.raboof.com/projects/jayrock/demo.ashx
The connection seems to work, but I keep getting this Exception
org.alexd.jsonrpc.JSONRPCException: Invalid JSON response
I've tried different methods and survey pages, but I always get the same Exception. Where am I going wrong?
The relevant code is below. AsyncTask is used because since 3.0 Android doesn't allow network connections in the main stream. Thanks in advance.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
JSONHandler task = new JSONHandler();
task.execute(new String[] {"http://www.raboof.com/projects/jayrock/demo.ashx"});
}
private class JSONHandler extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
for (String url : urls) {
JSONRPCClient client = JSONRPCClient.create(url);
client.setConnectionTimeout(2000);
client.setSoTimeout(2000);
try {
client.call("counter");
} catch (JSONRPCException e) {
e.printStackTrace(); //Invalid JSON Response caught here
}
}
return null;
}
}
I have tested your system using the last version of the library. It work great. You need to us callInt("counter") and it will be ok.
There is the code I used:
public JSONRPCClient client = JSONRPCClient.create("http://www.raboof.com/projects/jayrock/demo.ashx", JSONRPCClient.Versions.VERSION_2);
try{
int resInt = client.callInt("counter");
} catch (JSONException e) {
Log.i("JSON-RPC Client", e.toString());
}
I hope this can help.
PS: with this new version, you use parameters send as an array, or using a JSONObject to send named parameters. This is only possible if using the version 2.0 of the JSON-RPC protocol.
This is the only JSON-RPC client I've been able to get to work with Zend_Json_Server on Android (and I've tried a few).
Make sure to set the version to 2.0 also, as this client doesn't work unless your server is explicitly using the 2.0 spec:
$server = new Zend_Json_Server();
$server->setClass('My_Class');
$server->getRequest()->setVersion("2.0");
if ('GET' == $_SERVER['REQUEST_METHOD']) {
// Indicate the URL endpoint, and the JSON-RPC version used:
$server->setTarget('/ajax.php')
->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
// Grab the SMD
$smd = $server->getServiceMap();
// Return the SMD to the client
header('Content-Type: application/json');
echo $smd;
return;
}
$server->handle();

fail to retrieve request token for yahoo integration in android?

I am trying to integrate yahoo in my app. I am trying,
private static final String YAHOO_CALLBACK_URI = "MyApp://oauth";
public static final String YAHOO_REQUEST_TOKEN_URL = "http://api.login.yahoo.com/oauth/request_token";//api.login.yahoo.com
public static final String YAHOO_ACCESS_TOKEN_URL = "http://api.login.yahoo.com/oauth/access_token";
public static final String YAHOO_AUTHORIZE_URL = "http://api.login.yahoo.com/authorize";
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(YAHOO_CONSUMER_KEY, YAHOO_CONSUMER_SERECT_KEY);
OAuthProvider provider = new CommonsHttpOAuthProvider(YAHOO_REQUEST_TOKEN_URL, YAHOO_ACCESS_TOKEN_URL, YAHOO_AUTHORIZE_URL);
provider.setOAuth10a(true);
try {
String authUrl = provider.retrieveRequestToken(consumer, YAHOO_CALLBACK_URI);// Can I pass Null for callback url.
System.out.println("AuthURL = " + authUrl);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
getting
oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Service provider responded in error: 404 (Not Found)
Why we are unable to retrieve request token.
Make sure your app on Yahoo is registered as Web-Based. When you register as a Web-Based application, give a valid Application URL and App Domain of your choice (both should be the same domain), but has not been used by anyone else. I, for example, have the url of a website I own. This will also be used as the callback url in your consumer.
Instead of calling "MyApp://oauth", give a valid http callback url (the same one you used to register the app). With that much, your code above should work.
Open a new Webview and call the authUrl. Once your app is authorised, intercept the call to your application url by overriding the "onPageStarted" method. There, make a callback to your app by launching a new intent pointing to "MyApp://oauth".
Please let me know if any of the obove steps confuse you - I am more than happy to help.
I had followed the above mentioned 3#poits and able to do the call back to the application. The whole problem started with YAHOO OAUTH Process. Now Yahoo doesnt take the Custom Call back urls, It has to be a valid http url. n
There are couple of issues i have it which i am solving on my own,like Having a CUSTOMi Dialog box which can be used to call the Web-view YAHOO Auth URL similar to FB Dialog.
Thanks for the

Categories

Resources