Image loading using picasso library from url - android

initially, I am fetching image URL from server to a string then using Picasso library I am trying to load the image
I am able to get image URL from the server like thislogcat
but image not loaded in the image view. when tried placing direct URL it works.
public class MainActivity extends AppCompatActivity {
ImageView im;
Button bm;
String str ;
private static String NAMESPACE = "http://telview360/";
private static String URL = "http://54.179.134.139/viView360Service/WebService.asmx?WSDL";
private static String SOAP_ACTION = "http://telview360/ImageDetails";
private static String METHOD_NAME = "ImageDetails";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_image);
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
bm = (Button) findViewById(R.id.btn_load_image);
bm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
im = (ImageView) findViewById(R.id.image);
Picasso.with(getApplicationContext()).load(str).into(im);
}
});
}
}

add this to your code
final Thread networkThread = new Thread() {
#Override
public void run() {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.call(SOAP_ACTION, envelope);
final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
str = response.toString();
Log.d("Webservice", " response " + str);
} catch (Exception e) {
e.printStackTrace();
}
}
};
networkThread.start();
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
it will work

Related

org.xmlpull.v1.XmlPullParserException: expected: START_TAG in api calling

I am new bee in android. I am trying parse data using soap api. Following is my snippet code. When I run the project I am getting following error can any one help me with this? Following is my snippet code. I am not able to find the issue what exactly I am facing. sorry for bad english.
Error
W/System.err: org.xmlpull.v1.XmlPullParserException: expected:
START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope
(position:START_TAG #4:44 in java.io.InputStreamReader#7f5b56a)
Code
public class GetReport extends Activity {
private static final String NAMESPACE = "https://www.myweb.co.ke/Wt/"; // com.service.ServiceImpl
private static final String URL = "https://www.myweb.co.ke/Wt/webtask.asmx";
private static final String METHOD_NAME = "GetProductListing";
private static final String SOAP_ACTION = NAMESPACE+METHOD_NAME;
private String webResponse = "";
private Handler handler = new Handler();
private Thread thread;
private TextView textView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(1);
setContentView(R.layout.test_demo);
textView1 = (TextView) findViewById(R.id.ttte);
startWebAccess("title");
}
public void startWebAccess(String a) {
final String aa = a;
thread = new Thread() {
public void run() {
try {
Log.d("Req value0R", "Starting...");// log.d is used for
// debug
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
/* request.addProperty("ClientCode", "64396");
request.addProperty("key", "Om$#!##M^#R");*/
request.addProperty("User Name", "1234");
request.addProperty("Password", "4321");
Log.d("Req value1", request.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(
URL);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject objectResult = (SoapObject) envelope.bodyIn;
webResponse = objectResult.toString();
System.out.println("response: " + webResponse);
} catch (SoapFault sp) {
sp.getMessage();
System.out.println("error = " + sp.getMessage());
} catch (Exception e) {
System.out.println("problem8");
e.printStackTrace();
webResponse = "Connection/Internet problem";
}
handler.post(createUI);
}
};
thread.start();
}
final Runnable createUI = new Runnable() {
public void run() {
if (webResponse != null) {
textView1.setText(webResponse);
} else {
webResponse = "No data provided presently";
textView1.setText(webResponse);
}
}
};
}

android - connect to web service

I'm try to connect android application to a web service uploaded on somee.
I have the following problem :
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException
the web service code :
[WebService(Namespace = "http://www.n-m.somee.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
SqlConnection con = new SqlConnection(#"workstation id=WSDatabase.mssql.somee.com;packet size=4096;user id=***;pwd=***;data source=WSDatabase.mssql.somee.com;persist security info=False;initial catalog=WSDatabase ");
[WebMethod]
public string get_name(String pssw)
{
con.Open();
SqlCommand cmd = new SqlCommand("select username from users where pssw="+pssw, con);
string rd =(string) cmd.ExecuteScalar();
con.Close();
return rd;
}
}
I added the following line to manifest file:
** MainActivity.java file**
public class MainActivity extends AppCompatActivity {
protected String namespace="http://www.n-m.somee.com/";
protected String url="http://www.n-m.somee.com/WebService1.asmx";
protected String SOAPAction="http://www.n-m.somee.com/get_name";
protected String method_name= "get_name";
TextView user_name;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void goButton(View v)
{
user_name=(TextView)findViewById(R.id.textView1);
//allow internet
if(Build.VERSION.SDK_INT>9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
//create soap object
SoapObject soapObject=new SoapObject(namespace,method_name);
soapObject.addProperty("pssw", "1234");
//create envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
//Rrquest get data from webservice and set it into soapobject
HttpTransportSE transport = new HttpTransportSE(url);
try {
transport.call(SOAPAction, envelope);
// get data
SoapPrimitive output=(SoapPrimitive) envelope.getResponse();
//set data into control
user_name.setText("pssw: "+output.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
I appreciate any help
thank you
Your urls were a bit wrong. This is what I used,
protected String namespace = "http://www.nourhan-m.somee.com/";
protected String url = "http://www.nourhan-m.somee.com/WebService1.asmx";
protected String SOAPAction = "http://www.nourhan-m.somee.com/get_name";
protected String method_name = "get_name";
I made some modification to your code to make it work.
public String callService(String username) {
//create soap object and add params to it
SoapObject request = new SoapObject(namespace, method_name);
request.addProperty("pssw", username);
//create envelop
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//set request to envelope
envelope.bodyOut = request;
HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;
try {
transport.call(SOAPAction, envelope);
Log.e("OUT", transport.requestDump);
return transport.responseDump;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return "";
}
This is how I invoked the method,
You can invoke it as follows,
final String username = (TextView) findViewById(R.id.textView1)
new Thread(new Runnable() {
#Override
public void run() {
Log.e("OUT", callService(username));
}
}).start();
I got an empty response, maybe it's because I didn't give a valid username. Have a look at it. Cheers :)

Connecting to external webservice fail to response on Android

I'm trying to connect to a external webservice from an Android app, but currently it crashes on the XML response, please help me notice if the call structure is built incorrectly or any the response code is crashed. Here is my code:
public class sri extends AsyncTask<String, String, String>
{
public final static String URL = "http://qa-suia.ambiente.gob.ec:8092/suiawebservices/SuiaServices?wsdl";
public static final String NAMESPACE = "http://client.ambiente.gob.ec/";
public static final String SOAP_ACTION_PREFIX = "/";
private static final String METHOD = "getRuc";
private String resp;
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
protected String doInBackground(String... param)
{
SoapObject request = new SoapObject(NAMESPACE,METHOD);
//String JsonString=Serializa();
request.addProperty("user","99999999");
request.addProperty("pass","xxxxxxxxxxxx");
request.addProperty("rucNumber","99999999999");
//sobre.dotNet=true;
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte=new HttpTransportSE(URL);
transporte.call(NAMESPACE+METHOD, sobre);
} catch (Exception e)
{
String msn="error";
return msn;
}
try {
if (sobre != null) {
SoapPrimitive response = (SoapPrimitive)sobre.getResponse();
resp=response.toString();
}
else
{
}
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
protected void onProgressUpdate(Integer... values)
{
}
public void onPreExecute()
{
}
public void onPostExecute(String result)
{
String resultado=result;
}
Here's working code for what I'm doing calling a .Net Service (It looks like you're not putting your request into an envelope):
private static SoapPrimitive callProcServiceForScalar(String serviceMethod, String storedProc, String params) throws Exception {
String NAMESPACE = "http://" + YourIPAddress + "/";
String METHOD_NAME = getMethodName(serviceMethod);
String SOAP_ACTION = NAMESPACE + METHOD_NAME;
String URL = "http://" + YourIP + "/YourService.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("sPassword", sYourPassword);
request.addProperty("sData", sServerDB);
request.addProperty("sSP_Name", storedProc);
request.addProperty("sParam", params);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Enable the below property if consuming .Net service
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
numberOfBytesTransmitted = numberOfBytesTransmitted + StringToBytes(request.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);
SoapPrimitive returnable = null;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
returnable = (SoapPrimitive)envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Msg:" + e.getMessage() + "; SP:" + storedProc + "; Params: " + params + "; Method:" + METHOD_NAME);
}
return returnable;
}

WSDL calling from Android causes NetworkOnMainThreadException

Here is this sample code (MainActivity):
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.text1);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
} catch (Exception e) {
tv.setText("Problem: " + e.toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
When I run this code I got this message Problem: android.os.NetworkOnMainThreadException.
In the manifest.xml added the
<uses-permission android:name="android.permission.INTERNET" />
line.
Here
is the whole android project.
Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.
This exception is thrown when an android application attempts to perform a network operation on its main thread. You must run your code in AsyncTask. Your code will look something like this
You can declare resultString as an instance variable
class SoapTask extends AsyncTask<String, Void, Void> {
private Exception exception;
#Override
protected Void doInBackground(Void... arg0) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
resultString = "Status:" + (SoapPrimitive)soapEnvelope.getResponse();
} catch (Exception e) {
resultString = "Problem:" + e.toString();
}
}
protected void onPostExecute(Void result) {
tv.setText("Status: " + resultString);
}
}
Try this..
NetworkOnMainThreadException throws when you are performing network operation on its main thread.
So you have to run you network operations in AsyncTask
You can call AsyncTask like below
new SerializationConnection().execute(URL);
then SerializationConnection class
class SerializationConnection extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(urls[0]);
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
return "Status: " + resultString;
} catch (Exception e) {
return "Problem: " + e.toString();
}
}
protected void onPostExecute(String result) {
tv.setText(result);
}
}
You are making a server call in main thread.if your target version is 9
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
For Better way try to use AsynTask

Android Soap Web service Error behind Proxy Server

I have written a program for communicating with a web service and get response value. But when i debug the programme i end with requestDump=null at the line androidHttpTransport.call(SOAP_ACTION, envelope); Can some one tell me the reason for the error and what can i do for this
public class WebService extends Activity {
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
String celsius;
Button b;
TextView tv;
EditText et;
String res,resultval;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service);
et=(EditText)findViewById(R.id.editText1);
tv=(TextView)findViewById(R.id.Result);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String result=getFarenheit(et.getText().toString());
//tv.setText(result+"°F");
new service().execute();
}
});
}
private class service extends AsyncTask<Void, Void, String>{
#Override
protected String doInBackground(Void... arg0) {
celsius=et.getText().toString();
SoapObject request= new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo celsuiusPI= new PropertyInfo();
celsuiusPI.setName("Celsius");
celsuiusPI.setValue(celsius);
celsuiusPI.setType(String.class);
request.addProperty("XMLMarks",celsuiusPI);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = true;
envelope.enc = SoapSerializationEnvelope.ENC2003;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
SoapPrimitive response;
HttpTransportSE androidHttpTransport=new HttpTransportSE(URL);
try{
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
String dump= androidHttpTransport.requestDump.toString();
response=(SoapPrimitive)envelope.getResponse();
Toast.makeText(WebService.this, response.toString(), 20).show();
Log.i("WebService output", response.toString());
System.out.println("WebService Response"+response.toString());
Object res= response.toString();
resultval=(String) res;
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h){
String result=h;
tv.setText(result+"°F");
}
}
}
Just replace your service AsyncTask with this new one and see result:
code:
private class service extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... arg0) {
System.out.println("In DoIn Background");
// Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Use this to add parameters
request.addProperty("Celsius", txtCel.getText().toString());
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) envelope.bodyIn;
if (result != null) {
// Get the first property and change the label text
// txtFar.setText(result.getProperty(0).toString());
res = result.getProperty(0).toString();
} else {
Toast.makeText(getApplicationContext(), "No Response",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
protected void onPostExecute(String h) {
String result = h;
tv.setText(result + "°F");
}
}

Categories

Resources