android 中的 HttpUrlConnection 中的 statusCode 500 或 400

statusCode 500 or 400 in HttpUrlConnection in android

我试了一个星期也没找到解决方法

总是出错
HttpUrlConnection

状态代码 500,有时状态代码 400

我试试

JsonCallBack

但没有返回数据

myurl 与浏览器配合良好并获取数据

但是 android 我的代码没有什么问题

我的代码:

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 文件:

<?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>

这个例子和我的 Url 在浏览器中运行良好

http://yar22222-001-site1.dtempurl.com/Sallyer.asmx/Test?UserName=maaaan&UserPhone=65356&addres=any place&TotalPrice=0.0

您可以使用 Android 的 Volley 来请求 url 并使用 GSON 库解析它。这是一种更有效的方法。

尝试这样使用这将解决你的问题

public String getimageurl(String id){


    String url="http://www.abc1234.com/wp-json/wp/v2/media/"+id;

    RequestQueue requestqueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringrequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                imageurl=response;
                JSONObject jsonObject = new JSONObject(response);
                String j = jsonObject.getString("guid");
                jsonObject = new JSONObject(j);
                imageurl =jsonObject.getString("rendered");

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestqueue.add(stringrequest);

    return imageurl;
}