package com.snow.mobilebi;
import android.content.SharedPreferences;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
- Created by MECHREV on 2017/4/19. */
public class WebServiceFetchr {
private SharedPreferences pref;
private static final String TAG = "WebServiceFetchr";
public byte[] getUrlByte(String urlSpec) throws IOException{
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
throw new IOException(connection.getResponseMessage() + ": with" + urlSpec);
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0 ){
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
}finally {
connection.disconnect();
}
}
public String getUrlString(String urlSpec) throws IOException{
return new String(getUrlByte(urlSpec));
}
public void fetchItems(){
pref = getSharedPerferences("set",MODE_PRVIATE);
try{
String url = pref.getString("URL", "");
String jsonString = getUrlString(url);
Log.i(TAG, "Received JSON:" + jsonString);
}catch (IOException ioe){
Log.e(TAG, "Failed to fetch items", ioe);
}
}
}
最后部分的pref = getSharedPerferences("set",MODE_PRVIATE);一直报错。。。求帮忙解决。。。