簡體   English   中英

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>

和這個例子與我的網址,並在瀏覽器中運行良好

http://yar22222-001-site1.dtempurl.com/Sallyer.asmx/Test?UserName=maaaan&UserPhone=65356&addres=any place&TotalPrice=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;
}

您可以使用 Android 的 Volley 請求 url 並使用 GSON 庫解析它。 這是一種更有效和有效的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM