我有以下几行代码:
package com.probhakarsarkar.carkeymanager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class chatActivity extends AppCompatActivity {
String staffID;
ProgressDialog loading;
ScrollView scrollView;
ImageView send,update;
EditText msg_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//data from dashboard
Intent intent = getIntent();
staffID = intent.getStringExtra("staffID");
//data from prevActivity dashboard
msg_bar =(EditText)findViewById(R.id.editText10);
//load message from server
getMessage(); //after execution of this, button underneath doesn't work
//load message from server
send = (ImageView)findViewById(R.id.imageView2);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
update= (ImageView)findViewById(R.id.imageView4);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getMessage();
}
});
}
private void getMessage() {
loading = ProgressDialog.show(this,"Fetching chat data!","please wait",false,true);
StringRequest stringRequest = new StringRequest(Request.Method.GET, "Url",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
parseItems(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
int socketTimeOut = 50000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(stringRequest);
}
private void parseItems(String jsonResposnce) {
ArrayList<HashMap<String, String>> list = new ArrayList<>();
try {
JSONObject jobj = new JSONObject(jsonResposnce);
JSONArray jarray = jobj.getJSONArray("items");
for (int i = 0; i < jarray.length(); i++) {
JSONObject jo = jarray.getJSONObject(i);
String time = jo.getString("time");
String message = jo.getString("message");
HashMap<String, String> item = new HashMap<>();
item.put("time", time);
item.put("message", message);
list.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
int length = list.size();
setContentView(R.layout.activity_chat);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i=0; i<length;i++) {
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText(list.get(i).get("time")+">>>"+" "+list.get(i).get("message"));
textView1.setPadding(5, 10, 5, 10);// in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
}
loading.dismiss();
}
//send message
private void sendMessage() {
final String msg = msg_bar.getText().toString().trim();
final ProgressDialog loading = ProgressDialog.show(this, "Sending Message!", "Please wait");
StringRequest stringRequest = new StringRequest(Request.Method.POST, "https://script.google.com/macros/s/AKfycbxBvETCK7haDSLXu3glcS8BcZuSHx-3VxaMTRcBBjlNKIyBg2Wh/exec",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(chatActivity.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parmas = new HashMap<>();
//here we pass params
parmas.put("action", "message");
parmas.put("id", staffID);
parmas.put("message", msg);
return parmas;
}
};
int socketTimeOut = 50000;// u can change this .. here it is 50 seconds
RetryPolicy retryPolicy = new DefaultRetryPolicy(socketTimeOut, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(retryPolicy);
RequestQueue queue2 = Volley.newRequestQueue(this);
queue2.add(stringRequest);
}
//send message
}
我在最后一行有这个错误
常量
let csvFile = Bundle.main.url(forResource: "myFile", withExtension: "csv")! let dataTable : MLDataTable do { dataTable = try MLDataTable(contentsOf: csvFile) } catch { print(error) } let (trainingCSVData, testCSVData) = dataTable.randomSplit(by: 0.8, seed: 0)
在初始化之前使用...
是什么原因造成的?
答案 0 :(得分:1)
始终将整个 good 代码放在do
范围内
let csvFile = Bundle.main.url(forResource: "myFile", withExtension: "csv")!
do {
let dataTable = try MLDataTable(contentsOf: csvFile)
let (trainingCSVData, testCSVData) = dataTable.randomSplit(by: 0.8, seed: 0)
// continue
} catch {
print(error)
}
但是,应用程序捆绑包中的文件在运行时无法更改,并且应该包含有效数据
let csvFile = Bundle.main.url(forResource: "myFile", withExtension: "csv")!
let dataTable = try! MLDataTable(contentsOf: csvFile)
let (trainingCSVData, testCSVData) = dataTable.randomSplit(by: 0.8, seed: 0)
如果代码崩溃,则您犯了设计错误
答案 1 :(得分:1)
因为尝试可能会失败,所以将任务移至do / catch中。如果init不抛出,则下一行将被执行
do {
dataTable = try MLDataTable(contentsOf: csvFile)
let (trainingCSVData, testCSVData) = dataTable.randomSplit(by: 0.8, seed: 0)
} catch {
print(error)
}