rubiks.kde
یک شنبه 06 مرداد 1392, 00:42 صبح
سلام دوستان
من چند روزیه یه مشکل حسابی کلافم کرده اونم این که یه برنامه دارم که از سرور یه سری اطلاعات رو میگیره این اطلاعات توسط gzip توی سرور فشرده میشن و من توی برنامه اونا رو از فشرده گی خارج و استفاده میکنم به این صورت :
public JSONObject getInfo(String url){
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
get.addHeader("Accept-Encoding", "gzip");
try {
HttpResponse response = client.execute(get);
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding.getValue().equalsIgnoreCase("gzip")) {
try {
instream = new GZIPInputStream(instream);
final int BUFFER_SIZE = 32;
GZIPInputStream gis = new GZIPInputStream(instream, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
String str = string.toString();
JSONObject jobj = new JSONObject(str);
return jobj;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
instream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
ولی مشکل اینه که این کد زمانی برام درست کار میکنه که یا با wifi وصل شده باشم یا gprs بدون پروکسی
مثلا اگه تنطیمات ایرانسل برای gprs رو دیده باشید یه پروکسی و پورت داره که اگه این دو مورد ست شده باشن این تابع درست کار نمیکنه و توی خط
GZIPInputStream gis = new GZIPInputStream(instream, BUFFER_SIZE);
خطا میده که فرمت درست نیست و باید خط زیر رو کامنت کنم تا درست بشه
instream = new GZIPInputStream(instream);
ولی موقعی که این خط رو کامنت میکنم بعد با wifi وصل میشم همون خطای بالا رو میده و باید این خط رو بزارم.
این مشکل این چند روز حسابی کلافم کرده اگه دوستان کمک کنن بسیار ممنون میشم.
من چند روزیه یه مشکل حسابی کلافم کرده اونم این که یه برنامه دارم که از سرور یه سری اطلاعات رو میگیره این اطلاعات توسط gzip توی سرور فشرده میشن و من توی برنامه اونا رو از فشرده گی خارج و استفاده میکنم به این صورت :
public JSONObject getInfo(String url){
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
get.addHeader("Accept-Encoding", "gzip");
try {
HttpResponse response = client.execute(get);
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding.getValue().equalsIgnoreCase("gzip")) {
try {
instream = new GZIPInputStream(instream);
final int BUFFER_SIZE = 32;
GZIPInputStream gis = new GZIPInputStream(instream, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
String str = string.toString();
JSONObject jobj = new JSONObject(str);
return jobj;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} finally {
instream.close();
}
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
ولی مشکل اینه که این کد زمانی برام درست کار میکنه که یا با wifi وصل شده باشم یا gprs بدون پروکسی
مثلا اگه تنطیمات ایرانسل برای gprs رو دیده باشید یه پروکسی و پورت داره که اگه این دو مورد ست شده باشن این تابع درست کار نمیکنه و توی خط
GZIPInputStream gis = new GZIPInputStream(instream, BUFFER_SIZE);
خطا میده که فرمت درست نیست و باید خط زیر رو کامنت کنم تا درست بشه
instream = new GZIPInputStream(instream);
ولی موقعی که این خط رو کامنت میکنم بعد با wifi وصل میشم همون خطای بالا رو میده و باید این خط رو بزارم.
این مشکل این چند روز حسابی کلافم کرده اگه دوستان کمک کنن بسیار ممنون میشم.