PDA

View Full Version : مشکل در خواندن URLConnection



roozkhosh_roozkhosh
دوشنبه 01 دی 1393, 08:57 صبح
با سلام
من از این کد برای خواند یک آدرس وب استفاده می گنم ولی فقط 3614 کارکتر از فایل خوانده می شود.اگه کسی تا حالا این مشکل را حل کرده ممنون می شوم راهنمایی کند.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

public class GetData extends AsyncTask<Void, Void, String> {
String Parameter, Act;
String TAG = "HamidDebug";
public String response;

GetData(String Act, String Parameter) {

this.Act = Act;
this.Parameter = Parameter;
response = "<Fail>Unknown Error</Fail>";

}

@Override
protected String doInBackground(Void... arg0) {

HttpURLConnection urlConnection = null;

try {
String Param = "Act=" + Act + "&" + Parameter;
URL url = new URL(
"http://androidws.bndzabansara.com/FetchData.aspx?" + Param);
Log.d(TAG, "Try to open: " + url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
Log.d(TAG, "Response code is: " + responseCode);

if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));

if (in != null) {
StringBuilder strBuilder = new StringBuilder();
int ch = 0;
Integer Count = 0;
while ((ch = in.read()) != -1) {
strBuilder.append((char) ch);
Count++;
}
response = strBuilder.toString();
Log.e(TAG, String.valueOf(Count));
}

in.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

finally {
urlConnection.disconnect();
}
return response;
}

}