我正在尝试发出http请求以连接应用程序并执行身份验证过程。但是我得到了 NullpointerException 。连接对象无法获取流数据。这是导致空指针异常。这是我的代码
String api="URL of the application";
String query="format=json";
//Necessary Variables
String publicKey=publickey;
String privateKey=privatekey;
String email=uemail;
String password=upassword;
String algo="HmacSHA1";
//identify Application
long time=System.currentTimeMillis() / 1000;
SecretKeySpec sks=new SecretKeySpec(privateKey.getBytes(),algo);
try {
Mac mac=Mac.getInstance(algo);
mac.init(sks);
String hash="";
byte[] digest=mac.doFinal((query+time).getBytes());
for (byte b : digest) {
hash +=String.format("%02x", b);
}
//Make the request
String url=api+"?"+query+"&apikey="+publicKey+"&hash="+hash+"&t="+time;
Log.d("URL", "url: "+url);
try {
HttpsURLConnection c=(HttpsURLConnection) new URL(url.trim()).openConnection();
c.setRequestProperty("Authorization","Basic "+DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));
InputStream iStream=null;
try {
***iStream=c.getInputStream();***
} catch (Exception e) {
iStream=c.getErrorStream();
}
InputStreamReader iStreamReader=new InputStreamReader(iStream);
BufferedReader bReader=new BufferedReader(iStreamReader);
String result="";
String line="";
while ((line=bReader.readLine())!=null) {
result +=line+"\n";
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是例外。
10-17 11:09:38.127: E/AndroidRuntime(864): FATAL EXCEPTION: main
10-17 11:09:38.127: E/AndroidRuntime(864): java.lang.NullPointerException
10-17 11:09:38.127: E/AndroidRuntime(864): at libcore.net.http.HttpEngine.writeRequestHeaders(HttpEngine.java:646)
10-17 11:09:38.127: E/AndroidRuntime(864): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:800)
10-17 11:09:38.127: E/AndroidRuntime(864): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
10-17 11:09:38.127: E/AndroidRuntime(864): at libcore.net.http.HttpURLConnectionImpl.getErrorStream(HttpURLConnectionImpl.java:100)
10-17 11:09:38.127: E/AndroidRuntime(864): at libcore.net.http.HttpsURLConnectionImpl.getErrorStream(HttpsURLConnectionImpl.java:124)
10-17 11:09:38.127: E/AndroidRuntime(864): at com.arc.distimoapp.LoginScreen.loginDistimo(LoginScreen.java:113)
10-17 11:09:38.127: E/AndroidRuntime(864): at com.arc.distimoapp.LoginScreen$1.onClick(LoginScreen.java:62)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.view.View.performClick(View.java:4084)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.view.View$PerformClick.run(View.java:16966)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.os.Handler.handleCallback(Handler.java:615)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.os.Handler.dispatchMessage(Handler.java:92)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.os.Looper.loop(Looper.java:137)
10-17 11:09:38.127: E/AndroidRuntime(864): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-17 11:09:38.127: E/AndroidRuntime(864): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 11:09:38.127: E/AndroidRuntime(864): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 11:09:38.127: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-17 11:09:38.127: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-17 11:09:38.127: E/AndroidRuntime(864): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我的代码中的一些修改使我实现目的地。 首先,我在catch body中添加e.printstacktrack。
try {
Log.d(TAG, "Trying to get the input stream..");
iStream=c.getInputStream();
} catch (Exception e) {
e.printStackTrace();
iStream=c.getErrorStream();
}
让我知道新的例外情况。
android.os.NetworkOnMainThreadException ..............
通过将代码放入线程
来解决此异常 Thread t = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
HttpsURLConnection c=(HttpsURLConnection) new URL(url.trim()).openConnection();
Log.d(TAG, "Parameter value is " + "Basic " + DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));
c.setRequestProperty("Authorization","Basic "+DatatypeConverter.printBase64Binary((email+":"+password).getBytes()));
InputStream iStream=null;
try {
Log.d(TAG, "Trying to get the input stream..");
iStream=c.getInputStream();
} catch (Exception e) {
e.printStackTrace();
iStream=c.getErrorStream();
}
Log.d(TAG, "Trying to read...");
InputStreamReader iStreamReader=new InputStreamReader(iStream);
Log.d(TAG, "Buffering...");
BufferedReader bReader=new BufferedReader(iStreamReader);
String result="";
String line="";
Log.d(TAG, "About to parse results...");
while ((line=bReader.readLine())!=null) {
Log.d(TAG, "Line is " + line);
result +=line+"\n";
}
Log.d(TAG, "Output is: " + result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.run();
}
};
t.start();
现在工作正常。