我正在尝试发出 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)