i try for one week without find solution
always get error with
HttpUrlConnection
status Code 500 and some times status Code 400
I try
JsonCallBack
but no data back
myurl work with Browser fine and get Data
but in android nothing what the wrong in my code
My code :
public class AcceptOrder extends AppCompatActivity {
EditText Names;
EditText No;
EditText Adres;
AQuery aQuery;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accept_order);
Names = (EditText) findViewById(R.id.name);
No = (EditText) findViewById(R.id.phoneNo);
Adres = (EditText) findViewById(R.id.adress);
aQuery = new AQuery(this);
handler = new Handler();
// final LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
// if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// buildAlertMessageNoGps();}
}
public void FinishedOrder(View view) {
String UserNAme = Names.getText().toString();
String PhoneNo = No.getText().toString();
String Addr = Adres.getText().toString();
if((UserNAme.length()<2 )) {
Operations.DisplayMessage(this, getResources().getString(R.string.AddAllinfo));
return;
} else {
UserSetting.url1=UserSetting.ServerURL+"Sallyer.asmx/Test?UserName="+ UserNAme+
"&UserPhone="+PhoneNo+
"&addres="+Addr+"&TotalPrice="
+UserSetting.TotalPrice;
new MyAsyncTaskgetNews().execute();
}
}
public void JsonCallBack (String url , JSONObject json , AjaxStatus status) throws JSONException {
if (json != null)
{
String msg = json.getString("Message");
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
int Isadd=json.getInt("IsAdd");
if(Isadd==1) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
} else {
}
}
public class MyAsyncTaskgetNews extends AsyncTask<String, String, String> {
String Datacame ;
@Override
protected void onPreExecute() {
//before works
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String myurl=UserSetting.url1;
try{
URL url = new URL(myurl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
//int status = urlConnection.getResponseCode();
try {
InputStream in;//= new BufferedInputStream(urlConnection.getInputStream());
int statusCode = urlConnection.getResponseCode();
if (statusCode >= 200 && statusCode < 400) {
// Create an InputStream in order to extract the response object
in = urlConnection.getInputStream();
} else {
in = urlConnection.getErrorStream();
}
Datacame = Operations.ConvertInputToStringNoChange(in);
urlConnection.disconnect();
} catch (Exception ex) {
ex.getMessage();
}
// read jsn data
final JSONObject json = new JSONObject(Datacame);
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the UI
@Override
public void run() {
if (json != null)
{
int IsAdd= 0;
try {
IsAdd = json.getInt("IsAdd");
} catch (JSONException e) {
e.printStackTrace();
}
if(IsAdd==1) {
}
}
}
});
}
};
new Thread(runnable).start();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}catch (Exception ex) {
ex.getMessage();
}
return null;
}
protected void onPostExecute(String result2){
}
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/name"
android:layout_weight="1" />
<TextView
android:text="@string/Name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView6"
android:layout_weight="1"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/phoneNo"
android:layout_weight="1" />
<TextView
android:text="@string/PhoneNo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView8"
android:layout_weight="1"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/adress"
android:layout_weight="1" />
<TextView
android:text="@string/Adress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView9"
android:layout_weight="1"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:text="@string/GPS"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button2"
android:layout_weight="1"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:text="@string/Finished"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button3"
android:layout_weight="1"
android:textSize="24sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:gravity="center"
android:onClick="FinishedOrder" />
</LinearLayout>
</LinearLayout>
and this Example with my Url and work good in browser
http://yar22222-001-site1.dtempurl.com/Sallyer.asmx/Test?UserName=maaaan&UserPhone=65356&addres=any place&TotalPrice=0.0
You can use Android's Volley for requesting url and parse it using GSON library. This is a more efficient and effective approach.