我正在为一家与mysql数据库通信的公司构建一个应用程序,我正在尝试向服务器发送一个新条目,但我无法获得
outputstream write
方法工作,我知道有一个错误,但我似乎找不到解决办法
这是我的密码:
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new OutputStream() {
@Override
public void write(int b) throws IOException {
Log.e("Car DetailStream Writer","outputStreamWriter write method called");
try {
int isLargeAsInt = car.isLarge() ? 1 : 0;
int isDieselAsInt = car.isDiesel() ? 1 : 0;
Log.e("Car Details Write Data",car.getCarModel());
writeData =
URLEncoder.encode("carModel", "UTF-8") + "=" + URLEncoder.encode(car.getCarModel(), "UTF-8") + "&" +
URLEncoder.encode("carYear", "UTF-8") + "=" + URLEncoder.encode(car.getYear(), "UTF-8") + "&" +
URLEncoder.encode("carPlate", "UTF-8") + "=" + URLEncoder.encode(car.getPlateNumber(), "UTF-8") + "&" +
URLEncoder.encode("isLarge", "UTF-8") + "=" + isLargeAsInt + "&" +
URLEncoder.encode("isDiesel", "UTF-8") + "=" + isDieselAsInt + "&" +
URLEncoder.encode("currentMileage", "UTF-8") + "=" + car.getCurrentMileage();
Log.e("Written Data", writeData);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
Log.d("Buffered Writer","Buffered Writer Created");
bufferedWriter.write(writeData);
bufferedWriter.flush();
下面是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at java.io.Writer.write(Writer.java:157)
at com.example.trknil.turknilmanagement.utils.SendUtils.makeHttpRequest(SendUtils.java:124)
这是第124行:
bufferedWriter.write(writeData);
谁能把代码修好,告诉我哪里出错了。
暂无答案!
目前还没有任何答案,快来回答吧!