android中的HttpUrlConnection中的statusCode 500或400

时间:2017-02-05 20:34:19

标签: android httpurlconnection aquery

我尝试了一周没有找到解决方案

总是会出错

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

2 个答案:

答案 0 :(得分:0)

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

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;
}

答案 1 :(得分:-1)

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