如何使用Web服务处理Android应用程序中的Internet缓慢状态?

时间:2014-03-20 11:34:03

标签: android web-services android-ksoap2 socketexception

public class NewClass extends Activity

{
 public final String NAMESPACE = "**";
public final String URL = "***";

public final String SOAP_ACTION_1 = "**";
public final String METHOD_NAME_1 = "***";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        new FeaturesHomes().execute();
}
}



    public class FeaturedHomes extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mProgressDialog=ProgressDialog.show(getActivity(), "Wait", "Data Loading..");
        }
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub

            if (mAllMethods.check_Internet()==true) {

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_1); 
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,60000);

                try {
                    androidHttpTransport.call(SOAP_ACTION_1, envelope);
                    SoapObject mSoapObjectCompanyDetailResponse = (SoapObject)envelope.bodyIn;
                    Object re= null;
                    re = envelope.getResponse();
                    System.out.println("Response "+re.toString());
                    JSONObject mainJson = new JSONObject(re.toString());
                    JSONArray jsonArray = mainJson.getJSONArray(StaticValues.FeaturedCommunitiesArray);
                    JSONArray jArray = mainJson.getJSONArray("RateLog");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);
                        mGetFeaturedProperty = new GetFeaturedProperty();
                        mGetFeaturedProperty.setResidenceID(objJson.getString(StaticValues.ResidenceId));
                        String s=AllMethods.MakeWord(objJson.getString(StaticValues.ResidenceTitle));
                        mGetFeaturedProperty.setResidenceTitle(s);
                        mGetFeaturedProperty.setResidenceAddressStreet(objJson.getString(StaticValues.ResidenceAddress));
                        mGetFeaturedProperty.setResidenceCountry(objJson.getString(StaticValues.ResidenceCountry));
                        mGetFeaturedProperty.setResidenceDesc(objJson.getString(StaticValues.ResidenceDesc));
                        mGetFeaturedProperty.setResidenceAddressPincode(objJson.getString(StaticValues.ResidencePincode));
                        mGetFeaturedProperty.setResidenceState(objJson.getString(StaticValues.ResidenceState));
                        mGetFeaturedProperty.setResidenceCity(objJson.getString(StaticValues.ResidenceCity));
                        mGetFeaturedProperty.setResiLatitude(objJson.getString(StaticValues.ResiLatitude));
                        mGetFeaturedProperty.setResiLongitude(objJson.getString(StaticValues.ResiLongitude));
                        mGetFeaturedProperty.setImage(objJson.getString(StaticValues.Image));

                        mArrayListFeaturedProperties.add(mGetFeaturedProperty);

                    }
                    for (int j = 0; j < jArray.length(); j++) {
                        JSONObject objJson1 = jArray.getJSONObject(j);
                        GetFeaturedProperty mGetFeaturedProperty1 = new GetFeaturedProperty();
                        mGetFeaturedProperty1.setRating(objJson1.getString("OverAllRate"));
                        mArrayListTempFeaturedProperties.add(mGetFeaturedProperty1);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            else {
                mProgressDialog.dismiss();
                mAllMethods.ShowDialog(getActivity(), "Validate", "Network not Available");
            }


            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (mProgressDialog!=null) {
                mProgressDialog.dismiss();
            }

            if (mAllMethods.isNetError) {
                mAllMethods.ShowDialog(getActivity(), "Validation", "Your internet is slow");
            }
            else {
                System.out.println("Rating SIze "+mArrayListTempFeaturedProperties.size());
                System.out.println("Ac SIze "+mArrayListFeaturedProperties.size());
                if (mArrayListFeaturedProperties.size()>0) {

                     mMap.clear();
                        for (int i = 0; i < mArrayListFeaturedProperties.size(); i++) {
                              MarkerOptions markerOptions = new MarkerOptions();
                                double lat =Double.parseDouble(mArrayListFeaturedProperties.get(i).getResiLatitude());
                                double lng =Double.parseDouble(mArrayListFeaturedProperties.get(i).getResiLongitude());
                                final LatLng latLng = new LatLng(lat,lng);

                                GetFeaturedProperty firstEventInfo = new GetFeaturedProperty(latLng,mArrayListFeaturedProperties.get(i).getResidenceID(),mArrayListFeaturedProperties.get(i).getResidenceTitle());


                                  Marker firstMarker = placeMarker(firstEventInfo);

                                  eventMarkerMap = new HashMap<Marker, GetFeaturedProperty>();

                                  eventMarkerMap.put(firstMarker, firstEventInfo);

                                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat ,lng), 8.0f));
                        }
                        mNewadapter=new NewCustomAdapter(getActivity(),mArrayListFeaturedProperties,mArrayListTempFeaturedProperties);
                        mGridView.setAdapter(mNewadapter);

                }
            }


        }
    }

AllMethdos.java

public class AllMethods {

Activity mActivity;
ConnectivityManager mConnectivityManager;
NetworkInfo mNetworkInfo ;
boolean isNetError=false;
public AllMethods() {
    // TODO Auto-generated constructor stub
}

  public boolean check_Internet()
{
    mConnectivityManager= (ConnectivityManager)    mActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
     mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

    if(mNetworkInfo != null && mNetworkInfo.isConnectedOrConnecting())
    {
        isNetError = false;
        return true;
    }
    else
    {
        isNetError = true;
        return false;
    }
}

 }

亲爱的朋友,当我运行上面的代码,然后有时它工作正常但是当我的设备Internet变慢,那么它将花费很多时间,所以我已经检查条件,如果任何互联网错误然后显示对话框消息,但它现在不会显示,数据未加载(意味着空白)和proggres对话框解除并给我错误消息,如

03-20 18:06:23.337: W/System.err(20648): java.net.SocketTimeoutException: failed to connect to seniorhomes.worldexplorer.ca/208.91.198.99 (port 80) after 20000ms
03-20 18:06:23.337: W/System.err(20648):    at libcore.io.IoBridge.connectErrno(IoBridge.java:150)
03-20 18:06:23.337: W/System.err(20648):    at libcore.io.IoBridge.connect(IoBridge.java:112)
03-20 18:06:23.337: W/System.err(20648):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-20 18:06:23.337: W/System.err(20648):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
03-20 18:06:23.337: W/System.err(20648):    at java.net.Socket.connect(Socket.java:872)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:77)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
03-20 18:06:23.347: W/System.err(20648):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
03-20 18:06:23.357: W/System.err(20648):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
03-20 18:06:23.357: W/System.err(20648):    at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76)
03-20 18:06:23.357: W/System.err(20648):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:146)
03-20 18:06:23.357: W/System.err(20648):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
03-20 18:06:23.367: W/System.err(20648):    at pkg.android.rootways.retirement.JobEventDetailListActivitty$GetJobList.doInBackground(JobEventDetailListActivitty.java:405)
03-20 18:06:23.367: W/System.err(20648):    at pkg.android.rootways.retirement.JobEventDetailListActivitty$GetJobList.doInBackground(JobEventDetailListActivitty.java:1)
03-20 18:06:23.367: W/System.err(20648):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-20 18:06:23.367: W/System.err(20648):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-20 18:06:23.367: W/System.err(20648):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-20 18:06:23.377: W/System.err(20648):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-20 18:06:23.377: W/System.err(20648):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-20 18:06:23.377: W/System.err(20648):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-20 18:06:23.377: W/System.err(20648):    at java.lang.Thread.run(Thread.java:864)

1 个答案:

答案 0 :(得分:1)

由于某个时候您的服务器没有响应您从应用程序调用的请求,因此您需要依靠网络速度以及服务器速度来快速响应