我在Android上有一个客户端,在c#上有服务器.客户端向服务器发送消息,没关系.但是我需要服务器在指定目录中发送文件夹和文件的请求列表,并且不知道如何做,因为客户端没有从服务器获取任何消息.客户端的部分代码旨在监听不起作用,应用程序只是停止,直到我关闭服务器应用程序,然后它再次工作,但仍然没有读取任何东西.提前致谢
客户:
package com.app.client.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
public class my_activity extends Activity {
private TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.button1);
txt = (TextView)findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
connectSocket("Hello");
}
});
}
private void …Run Code Online (Sandbox Code Playgroud) 我有一个示例json:
{
"type": "persons",
"id": 2,
"attributes": {
"first": "something",
"second": "something else"
}
}
Run Code Online (Sandbox Code Playgroud)
我必须为它制作一个模式(使用JSON API规范和JSON模式文档):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"type": {
"type": "string",
"pattern": "^persons$"
},
"id": {
"type": "integer"
},
"attributes": {
"type": "object",
"properties": {...}
}
},
"required": ["type", "id", "attributes"]
}
Run Code Online (Sandbox Code Playgroud)
问题是:如果"类型"唯一可接受的值是"人",我应该使用模式模式(如上所述)还是枚举类似
"enum": ["persons"]
Run Code Online (Sandbox Code Playgroud)
我无法从文档中得到任何明确的答案,尽管在规范枚举中的示例用于单个值.你有什么看法?
我有这样的代码:
try:
return make_success_result()
except FirstException:
handle_first_exception()
return make_error_result()
except SecondException:
handle_second_exception()
return make_error_result()
Run Code Online (Sandbox Code Playgroud)
我想知道有什么办法可以实现这个目标:
try:
# do something
except Error1:
# do Error1 specific handling
except Error2:
# do Error2 specific handling
else:
# do this if there was no exception
????:
# ALSO do this if there was ANY of the listed exceptions (e.g. some common error handling)
Run Code Online (Sandbox Code Playgroud)
因此代码以下列顺序之一执行:
try > else > finally
try > except > ???? > finally
Run Code Online (Sandbox Code Playgroud)
编辑:我的观点是
????块应该在任何except块之后执行,这意味着它是错误处理的补充,而不是替换.
我有一个Android客户端和C#服务器,他们通过socket进行通信.我有这个问题 - 如果我在调试模式下运行我的客户端应用程序并在正确的位置放置一个断点 - 它完美地工作,但没有它它没有.客户端将图像的地址发送到服务器,服务器创建它的缩略图,将其转换为byte []并将其发回.客户端获取byte [],将其转换回图像并显示它.我还发现,当它没有得到正确的字节[]时,它的大小是2896,有时是1448,无论发送的数组的原始大小是多少.
这是客户:
private void connectSocket(String a){
try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
Socket socket = new Socket(serverAddr, 4444);
String message = a;
flag = 0;
if(a.indexOf(".jpg")>0){
flag = 1;
}
ListItems.clear();
if(!a.equalsIgnoreCase("get_drives"))){
//.....
}
if(!ListItems.isEmpty()){
if(ListItems.get(0).matches("^[A-Z]{1}:$")){
ListItems.set(0, ListItems.get(0)+"\\");
}
}
PrintWriter out = null;
BufferedReader in = null;
InputStream is = null;
try {
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
is = socket.getInputStream();
out.println(message);
String text = …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我使用一个按钮启动Camera应用程序并将图片保存到sdCard上按特定日期和时间命名的特定文件夹.当我对图片的名称进行硬编码时,它工作正常,但如果我试图在名称中输入日期则根本不起作用.
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), Constants.IMAGE_FOLDER_URI);
imagesFolder.mkdirs();
Date d = new Date();
CharSequence s = DateFormat.format("MM-dd-yy hh:mm:ss", d.getTime());
File image = new File(imagesFolder, s.toString() + ".jpg"); //this line doesn't work
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivity(imageIntent);
Run Code Online (Sandbox Code Playgroud)
如果我把:
s = "some_name";
Run Code Online (Sandbox Code Playgroud)
然后它工作,但我需要图像名称中的当前日期和时间.