بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
مشکل با webservice
سلام دوستان و بزرگواران.
من تو webservice مشکل دارم . سمت php درست درست کار میکنه و خروجی و چاپ میکنه ولی کدهای اندروید ارور 3- میده .
این خطایی هست که exeption میده .
ErrorAttempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object
reference
ممنون میشم راهنماییم بفرمایید.
public class ModuleWebservice {
public static final int PROTOCOL_EXCEPTION=-1;
public static final int IO_EXCEPTION=-2;
public static final int UNKNOWN_EXCEPTION=-3;
public interface Listener{
void onSuccess(String data);
void onFail(int statuseCode,Exception e);
}
private String cacheFileName;
private String url;
private ArrayList<NameValuePair> inputArguments;
private Listener listener;
private boolean enableCatch;
private String cacheDir;
private long cacheExpirTime;
private int connectionTimeOut;
private int socketTimeout;
public ModuleWebservice url(String url){
this.url=url;
return this;
}
public ModuleWebservice inputArguments(ArrayList<NameValuePair> value){
inputArguments=value;
return this;
}
public ModuleWebservice listener(Listener value){
listener=value;
return this;
}
public ModuleWebservice cacheDir(String value){
cacheDir=value;
return this;
}
public ModuleWebservice enableCatch(boolean value){
enableCatch=value;
return this;
}
public ModuleWebservice cacheExpirTime(long value){
cacheExpirTime=value;
return this;
}
public ModuleWebservice socketTimeout(int value){
socketTimeout=value;
return this;
}
public ModuleWebservice connectionTimeOut(int value){
connectionTimeOut=value;
return this;
}
public void read (){
String data=null;
if(enableCatch){
cacheFileName=creatCacheFileName();
data=readFromCache();
}
if(data==null){
readFromNet();
}else{
final String finalData = data;
G.handler.post( new Runnable() {
@Override
public void run() {
try {
listener.onSuccess( finalData );
return;
}catch (Exception e){};
}
});
}
}
public void readFromNet(){
Thread thread= new Thread( new Runnable() {
@Override
public void run() {
try{
HttpParams params=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params,connectionTimeOut);
HttpConnectionParams.setSoTimeout(params,socketTimeout);
HttpClient client=new DefaultHttpClient(params);
HttpPost request=new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(inputArguments));
HttpResponse httpResponse=client.execute(request);
final String data=streamToString(httpResponse.getEntity().getContent());
if(enableCatch){
saveToCatch( System.currentTimeMillis(),data);
}
if(listener!=null){
G.handler.post( new Runnable() {
@Override
public void run() {
try {
listener.onSuccess( data );
return;
}catch (Exception e){};
}
});
}
}
catch(ClientProtocolException e){
if(listener!=null){
listener.onFail(PROTOCOL_EXCEPTION,e);
}
}
catch (IOException e){
if(listener!=null){
listener.onFail(IO_EXCEPTION,e);
}
}
catch (Exception e){
if(listener!=null){
listener.onFail(UNKNOWN_EXCEPTION,e);
}
}
}
});
thread.start();
}
private String creatCacheFileName() {
String output=url;
for(NameValuePair input:inputArguments){
output+=";"+input.getName()+":"+input.getValue();
}
String sha1=sha1(output);
return sha1+".dat";
}
final private static char[] hexArray="0123456789ABCDEF".toCharArray();
private String bytesToHex(byte[] bytes ){
char[]hexChars=new char[bytes.length*2];
for(int j=0;j<bytes.length;j++){
int v=bytes[j]&0xFF;
hexChars[j*2]=hexArray[v>>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
return new String(hexChars);
}
private String sha1(String toHash){
String hash=null;
try{
MessageDigest digest= MessageDigest.getInstance("SHA-1");
byte[] bytes=toHash.getBytes("UTF-8");
digest.update(bytes,0,bytes.length);
bytes=digest.digest();
hash=bytesToHex(bytes);
}
catch (NoSuchAlgorithmException e){
e.printStackTrace();
}
catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return hash;
}
private String streamToString(InputStream inputStream){
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuffer=new StringBuilder();
String line =null;
try{
while((line=bufferedReader.readLine())!=null){
stringBuffer.append((line+"\n"));
}
}catch (IOException e){
e.printStackTrace();
}finally {
try{
inputStream.close();
}
catch(IOException e){
e.printStackTrace();
}
}
return stringBuffer.toString();
}
private void saveToCatch(long when ,String data){
ObjectOutputStream outputStream=null;
try{
outputStream=new ObjectOutputStream(new FileOutputStream(cacheDir+"/"+cacheFileName));
outputStream.writeLong(when);
outputStream.writeInt(data.length());
outputStream.write(data.getBytes());
}catch(StreamCorruptedException e){
e.printStackTrace();
}catch (FileNotFoundException e){
e.printStackTrace();}
catch (IOException e){
e.printStackTrace();
}finally{
if(outputStream!=null){
try{
outputStream.flush();
}catch (IOException e){
e.printStackTrace();
}
try{
outputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
private String readFromCache() {
ObjectInputStream inputStream=null;
try{
inputStream=new ObjectInputStream(new FileInputStream(cacheDir+"/"+cacheFileName));
long when=inputStream.readLong();
long now= System.currentTimeMillis();
if(now-when>cacheExpirTime*1000){
new File(cacheDir+"/catch.dat").delete();
return null;
}
int bytesLenth=inputStream.readInt();
byte[] buffer=new byte[bytesLenth];
inputStream.read(buffer,0,bytesLenth);
String output=new String(buffer);
return output;
}
catch(StreamCorruptedException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}finally {
try{
if(inputStream!=null){
inputStream.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}
return null;
}
}
webservice.url("www.myweb.ir/android/readCategory")
.listener( listener)
.read();
0
0
دوستان راهنمایی بفرمایید. اگر از webservice استفاده کردین؟ (6 سال پیش)
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .
