为了账号安全,请及时绑定邮箱和手机立即绑定

请问各位,跟着号码归属地的课程编写的代码,报错这个怎么回事?

/ 猿问

请问各位,跟着号码归属地的课程编写的代码,报错这个怎么回事?

Escall 2018-11-06 16:37:39

报错:E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.searchphone, PID: 30446
                  java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/text/Charsets;
                      at okio.Buffer.<clinit>(Buffer.kt:2212)
                      at okhttp3.ResponseBody.create(ResponseBody.java:210)
                      at okhttp3.internal.Util.<clinit>(Util.java:60)
                      at okhttp3.internal.Util.immutableList(Util.java:234)
                      at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:123)
                      at com.example.searchphone.business.HttpUntil.<init>(HttpUntil.java:30)
                      at com.example.searchphone.mvp.Presenter.MainPresenter.sendHttp(MainPresenter.java:41)
                      at com.example.searchphone.mvp.Presenter.MainPresenter.searchPhoneinfo(MainPresenter.java:36)
                      at com.example.searchphone.MainActivity.onClick(MainActivity.java:44)
                 

详细说明:报错显示错误主要在at com.example.searchphone.business.HttpUntil.<init>(HttpUntil.java:30)
                      at com.example.searchphone.mvp.Presenter.MainPresenter.sendHttp(MainPresenter.java:41)
                      at com.example.searchphone.mvp.Presenter.MainPresenter.searchPhoneinfo(MainPresenter.java:36)
                      at com.example.searchphone.MainActivity.onClick(MainActivity.java:44)

这几行,但是编写处理逻辑没有问题啊,请各位好心人看一下,谢谢!

代码:

package com.example.searchphone;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.searchphone.model.Phone;
import com.example.searchphone.mvp.Presenter.MainPresenter;
import com.example.searchphone.mvp.mvpMainView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, mvpMainView {
    EditText input_phone;
    Button search;
    TextView result_phone;
    TextView result_province;
    TextView result_server;
    TextView result_carrier;
    MainPresenter mainPresenter;
    ProgressDialog progressDialog;//进度条

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input_phone = (EditText) findViewById(R.id.input_phone);//引用部件
        search = (Button) findViewById(R.id.search);
        result_carrier = (TextView) findViewById(R.id.result_carrier);
        result_phone = (TextView) findViewById(R.id.input_phone);
        result_province = (TextView) findViewById(R.id.result_province);
        result_server = (TextView) findViewById(R.id.result_server);
        search.setOnClickListener(this);
        mainPresenter = new MainPresenter(this);
        mainPresenter.Click(this);
    }

    @Override
    public void onClick(View view) {           //错误的地方
        mainPresenter.searchPhoneinfo(input_phone.getText().toString());//将手机号码传入mainpresenter
    }

    //mvpMainView接口的方法
    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void updateView() {
        Phone phone = mainPresenter.getPhone();
        result_phone.setText("手机号码" + phone.getTelephone());
        result_province.setText("省份" + phone.getProvince());
        result_server.setText("运营商" + phone.getServer());
        result_carrier.setText("归属运营商" + phone.getCarrier());
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = ProgressDialog.show(this, "提示", "正在加载...", false);
        } else if (progressDialog.isShowing()) {
            progressDialog.setTitle("");
            progressDialog.setMessage("正在加载...");
        }
        progressDialog.show();
    }

    @Override
    public void hidenLoading() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}

控制模块

package com.example.searchphone.mvp.Presenter;


import com.example.searchphone.business.HttpUntil;
import com.example.searchphone.model.Phone;
import com.example.searchphone.mvp.mvpMainView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 *专注与mainactivity的逻辑
 */

public class MainPresenter extends BasePresenter{
    String murl="https;//tcc.taobao.com/cc/json/mobile_tel_segment.htm";
    mvpMainView mvpmainView;
    Phone mphone;
    public Phone getPhone(){
        return mphone;
    }
    public MainPresenter(mvpMainView mainView){
        mvpmainView=mainView;
        //与view进行交互
    }
    public void searchPhoneinfo(String phone){
        if(phone.length()!=11){
            mvpmainView.showToast("请输入正确的手机号码");
            return;
        }
        mvpmainView.showLoading();
        //接下来写http请求处理逻辑
        sendHttp(phone);                  //错误的地方
    }
    private void sendHttp(String phone){           
        Map<String,String>map=new HashMap<String,String>();
        map.put("tel",phone);
        HttpUntil httpUntil =new HttpUntil(new HttpUntil.HttpResponse(){  //错误的地方
            public void onSuccess(Object object){
                String json=object.toString();//将object转化为string
                int index=json.indexOf("{");//截取前端多余字符,indexOf方法可返回某个指定的字符串值在字符串中首次出现的位置
                json=json.substring(index,json.length());
                //jsonObject
               mphone= parseModelWithOrgjson(json);
                mvpmainView.hidenLoading();
                mvpmainView.updateView();
            }
            public void onFail(String error){
                mvpmainView.showToast(error);
                mvpmainView.hidenLoading();
            }
        });
        httpUntil.sendGetHttp(murl,map);
    }
   private Phone parseModelWithOrgjson(String json)//解析方法
   {
       Phone phone =new Phone ();
       try {
            JSONObject jsonObject=new JSONObject(json);
           String value=jsonObject.getString("telphone");
           phone.setTelephone(value);
           value=jsonObject.getString("province");
           phone.setProvince(value);
            value=jsonObject.getString("server");
           phone.setServer(value);
           value=jsonObject.getString("carrier");
           phone.setCarrier(value);
       } catch (JSONException e) {
           e.printStackTrace();
       }
       return phone;
   }
}

OK HTTP框架使用

package com.example.searchphone.business;


import android.os.Looper;

import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.util.Iterator;
import java.util.Map;

import android.os.Handler;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


/**
 * Created by 陈茂洋 on 2018/10/30.
 */

public class HttpUntil {
    String murl;
    Map<String, String> mparam;
    HttpResponse mresponse;
    Handler myhandler = new Handler(Looper.getMainLooper());
    private final OkHttpClient client = new OkHttpClient();   //错误的地方

    public interface HttpResponse {//回调接口,拿到返回的数据

        void onSuccess(Object object);

        void onFail(String error);


    }

    public HttpUntil(HttpResponse response) {
        mresponse = response;
    }

    //发送http
    public void sendPostHttp(String url, Map<String, String> param) {
        sendHttp(url, param, true);
    }

    //获取http
    public void sendGetHttp(String url, Map<String, String> param) {
        sendHttp(url, param, false);

    }

    //发送核心
    private void sendHttp(String url, Map<String, String> param, boolean isPost) {
        murl = url;
        mparam = param;
        //编写请求逻辑
        run(isPost);
    }

    //发送逻辑
    private void run(boolean isPost) {
        Request request = createRequest(isPost);
        //创建请求队列
        client.newCall(request).enqueue(new Callback() {//将request传递到队列中
            @Override
            public void onFailure(Call call, IOException e) {//回调方法
                if (mresponse != null) {
                    myhandler.post(new Runnable() {
                        public void run() {
                            mresponse.onFail("请求错误");
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if (mresponse == null)
                    return;
                myhandler.post(new Runnable() {
                    public void run() {
                        if (!response.isSuccessful()) {
                            mresponse.onFail("请求失败" + response);
                        } else {
                            try {
                                mresponse.onSuccess(response.body().string());
                            } catch (IOException e) {
                                e.printStackTrace();
                                mresponse.onFail("结果转换失败");

                            }
                        }
                    }
                });

            }
        });
    }

    //第一步完成client对象后,第二步设置request方法
    private Request createRequest(boolean isPost) {
        Request request;
        if (isPost) {
            MultipartBody.Builder resquestBodyBuilder;
            resquestBodyBuilder = new MultipartBody.Builder();//实现上传文件或参数
            resquestBodyBuilder.setType(MultipartBody.FORM);
            //便利map请求参数
            /*获得map的迭代器,用作遍历map中的每一个键值对
            Iterator是迭代器,map之前应该定义过,姑且认为是HashMap。
            <Entry<String,String>>表示map中的键值对都是String类型的。
            map.entrySet()是把HashMap类型的数据转换成集合类型
            map.entrySet().iterator()是去获得这个集合的迭代器,保存在iter里面*/
            Iterator<Map.Entry<String, String>> iterator = mparam.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                resquestBodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());//把参数添加到请求数据中
            }
            request = new okhttp3.Request.Builder().url(murl)
                    .post(resquestBodyBuilder.build()).build();//创建请求
        } else {//get的请求方式
            String urlstr = murl + "?" + MapParamToString(mparam);
            request = new Request.Builder().url(urlstr).build();
        }
        return request;
    }

    private String MapParamToString(Map<String, String> param) {//拼接地址和参数
        StringBuilder stringBuilder = new StringBuilder();
        Iterator<Map.Entry<String, String>> iterator = param.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            stringBuilder.append(entry.getKey() + "=" + entry.getValue() + "&");
        }
        String str = stringBuilder.toString().substring(0, stringBuilder.length() - 1);//去掉最后一个符号为&
        return str;
    }

}

Model

package com.example.searchphone.model;


public class Phone {
    String telephone;
    String province;
    String server;
    String carrier;
    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }



    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getCarrier() {
        return carrier;
    }

    public void setCarrier(String carrier) {
        this.carrier = carrier;
    }



    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

}



查看完整描述

目前暂无任何回答

添加回答

回复

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信