如何在改造中修复IOException

时间:2018-12-30 14:16:10

标签: android node.js sql-server retrofit rx-android

我一直在从事具有node.js后端并与sql server中的数据库通信的android项目,并且IOException不断向我显示:

caused by java.io.EOFException: \n not found: limit=0 content=…

这是整个堆栈跟踪:

W/System.err: java.io.IOException: unexpected end of stream on Connection{10.0.2.2:1433, proxy=DIRECT hostAddress=/10.0.2.2:1433 cipherSuite=none protocol=http/1.1}
                  at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:208)
                  at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                  at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
                  at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                  at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
                  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
                  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
                  at okhttp3.RealCall.execute(RealCall.java:92)
                  at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
                  at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
                  at io.reactivex.Observable.subscribe(Observable.java:12036)
                  at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:12036)
                  at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
                  at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:579)
                  at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
                  at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                  at java.lang.Thread.run(Thread.java:761)


W/System.err: Caused by: java.io.EOFException: \n not found: limit=0 content=…
W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:236)
                  at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
                  at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
                ... 29 more

我一直在看,正在阅读教程以确保我正确构建API和连接代码,并阅读其他有相同问题的人,然后做他们没有做的事情解决了我的问题。

这是我的node.js代码:

var postData = req.body; // get post params
       var userId = uid.v4();
       const userExistQuery = 'SELECT email, first_name, last_name FROM User WHERE email = @email AND category = @category AND password = @password';
    if(postData.category.valueOf() == new String("Seller").valueOf()){
        connection.request()
        .input('email', sql.VarChar(45), postData.email)
        .input('password', sql.VarChar(45), postData.password)
        .input('category', sql.VarChar(45), postData.category)
             .query(userExistQuery)
             .then((recordSet)=>{
             if(recordSet.rowsAffected == 1){
                res.json("Already registered as Seller.");
              }else{
                var streetNum = postData.streetNum;
                var buildingNum = postData.buildingNum;
                var apartmentNum = postData.apartmentNum;
                var town = postData.town;
                var governance = postData.governance;
                var floorNum = postData.floorNum;
                var poBox = postData.poBox;
                var zipCode = postData.zipCode;
                var creditInfo = postData.creditInfo;
                const addSellerQuery =
                'INSERT INTO Seller(seller_id, street#, building#, apartment#, town, governance, floor#, P.O_box, zip_code, credit_info, first_name, last_name, email, password, phone_number ,category) VALUES(@sellerId,@streetNum,@buildingNum,@apartmentNum,@town,@governance,@floorNum,@poBox,@zipCode,@creditInfo,@firstName,@lastName,@email,@password, @phoneNumber, @category)';
 connection.request()
 .input('sellerId', sql.VarChar(45), userId)
 .input('streetNum', sql.VarChar(45), postData.streetNum)
 .input('buildingNum', sql.VarChar(45), postData.buildingNum)
 .input('apartmentNum', sql.VarChar(45), postData.apartmentNum)
 .input('town', sql.VarChar(45), postData.town)
 .input('governance', sql.VarChar(45), postData.governance)
 .input('floorNum', sql.VarChar(45), postData.floorNum)
 .input('poBox', sql.VarChar(45), postData.poBox)
 .input('zipCode', sql.VarChar(45), postData.zipCode)
 .input('creditInfo', sql.VarChar(45), postData.creditInfo)
 .input('firstName', sql.VarChar(45), postData.firstName)
 .input('lastName', sql.VarChar(45), postData.lastName)
 .input('email', sql.VarChar(45), postData.email)
 .input('password', sql.VarChar(45), postData.password)
 .input('phoneNumber', sql.VarChar(45), postData.phoneNumber)
 .input('category', sql.VarChar(45), postData.category)
.query(addSellerQuery)
.then((recordAdded)=>{
  if(recordAdded.rowsAffected == 1){
    res.json(dataPost.firstName + "Registered as Seller.");
  }else{
    res.json(dataPost.firstName + "failed to register as Seller.");
  }})}

这是我的改造代码:

public static Retrofit getRetrofit() {
    if (retrofit == null) {
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(120, TimeUnit.SECONDS)
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:1433/")
                .client(okHttpClient)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
   }
    return retrofit;
}

此代码如何从api获取数据:

 compositeSubs.add(myAPI.buyerSignUp(buyer.getEmail(), buyer.getName().split(" ")[0]
                        , buyer.getName().split(" ")[1], buyer.getPassword(), buyer.bringType(), buyer.getPhoneNumber())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(s -> {   // onNext method
                            Log.d(TAG, "onPreExecute get data from api");
                            ((AuthListener) callBack).onSignUp(s, compositeSubs, false);
                            Log.d(TAG, "onPreExecute after get data from api");
                        }, throwable -> {   // onError method
                            Log.d(TAG, "onPreExecute getting api error");
                            throwable.printStackTrace();
                        }, () -> {          // onComplete method
                            Log.d(TAG, "onPreExecute after getting data from api");
                            Handler handler = new Handler(Looper.getMainLooper());
                            handler.post(() -> {
                                if (progressDialog.isShowing())
                                    progressDialog.dismiss();
                            });
                            Log.d(TAG, "completed");
                        }));

我希望看到新买家已添加到数据库中的Buyer表中,但是此异常阻止添加新买家。

1 个答案:

答案 0 :(得分:0)

您是否从这里尝试过解决方案? https://github.com/square/okhttp/issues/2738